I recently posted a solution for the numbers game on the Countdown TV show using a recursive technique. For those unfarmiliar with the rules…

  • A fixed set of numbers (usually 6) are chosen at random
  • A target value is set
  • The player must reach the target value using some or all of the numbers from the list of 6 random numbers using only the operators +, *, - and /. The use of brackets is also permitted

I have attached a new program that uses the itertools library and for loops. As with the last code the print statement

  • "Eval..." means the program has used BODMAS (e.g: 1+3*10 = 31 as 10*3 = 30 and 30+1 = 31) whereas
  • "Running sum..." produces a running sum of the totals (1+3*10 = 40 as 1+3 = 4, 4*10 = 40)
import itertools
import re
def unpack(method):
    string = method
    special = ["*","+","-","/"]
    list_sum = []
    list_special = []
    numbers = (re.findall(r"[\w']+", string))
    for char in string: 
        if char in special: 

    for index in range (len(numbers)-1):
        to_eval = numbers[index] + list_special[index] + numbers[index+1]
        list_sum.append(f'{to_eval} = {eval(to_eval)}')
        numbers[index+1] = str(eval(to_eval))

    return list_sum

def plus (x,y): return x+y, "+"
def minus (x,y): return x-y, "-"
def times (x,y): return x*y, "*"
def divide (x,y): return x/y, "/"

def solve():
    for numbers in list_sols:
        total = 0
        running = ""
        list_to_call = [order_ops1,order_ops2,order_ops3,order_ops4,order_ops5,order_ops6]
        for operator in list_to_call[len(numbers)-2]:
            zipped = itertools.zip_longest(numbers, operator)
            to_calculate = (list(zipped))
            running = ""
            total = 0
            for (index,item) in enumerate(to_calculate):
                if index == 0:
                    total += item[0]
                    next_operator = item[1]
                    running = str(item[0])
                    total, symbol = next_operator(total,item[0])
                    running += symbol + str(item[0])
                    if index < len(numbers)-1: next_operator = item[1]
            if eval(running)==target:
                print(f"Eval: {running}")
            if total == target:
                print (f'Running sum: {unpack(running)}')

def main():
    global list_sols
    global order_ops1, order_ops2, order_ops3, order_ops4, order_ops5, order_ops6
    global target 

    numbers = [100,50,75,25,3,6]
    target = 952

    list_sols = []
    for number in itertools.chain.from_iterable(itertools.permutations(numbers,i) for i in range(2,7)):

    order_ops1,order_ops2,order_ops3,order_ops4,order_ops5,order_ops6 = [],[],[],[],[],[]
    for number in itertools.chain.from_iterable(itertools.product([plus,minus,times,divide],repeat=i) for i in range(1,6)):
        if len(number) ==1: order_ops1.append(list(number))
        if len(number) ==2: order_ops2.append(list(number))
        if len(number) ==3: order_ops3.append(list(number))
        if len(number) ==4: order_ops4.append(list(number))
        if len(number) ==5: order_ops5.append(list(number))
        if len(number) ==6: order_ops6.append(list(number))

    if target in numbers:
        print (target)

if __name__=="__main__":
    exit_key = input("Press any key to exit...")

  • 1
    \$\begingroup\$ Why do you keep putting "countdown" in your question titles? What does that mean? \$\endgroup\$ Commented May 21, 2019 at 17:26
  • \$\begingroup\$ Its the name of a British TV game show (en.wikipedia.org/wiki/Countdown_(game_show)). I have tried solving the game show in different ways and thought it'd be nice to share the code with people in case they are working on a similar solution \$\endgroup\$
    – MrJoe
    Commented May 21, 2019 at 17:34


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.