Skip to content
herepete edited this page Jun 11, 2019 · 1 revision

Welcome to the Countdown wiki!

Introduction

This has been written in python3.7 purley for the breakpoint() which i find very useful in troubleshooting but will work in python3.6 as well.

I have benchmarked this with over 4000 runs.

It finds a match in 92.68% of those cases. In the rest (i.e 7.32%) 84.01% got within 1 of the target

There is at least 1 bit of logic to sort out example....

Your target is  532
Our large number(s) are  ['50', '100']
Our small number(s) are  ['9', '10', '3', '3']

Summary
We were 1 out
We tried to hit 532 the nearest we could get was 531 made up of 9 50 3 3 * + *
3 * 3 = 9
9 + 50 = 59
9 * 59 = 531

#maths wrong
#100 - 3 = 97
#9 - 3 = 6
#97 * 6 = 582
#582 - 50 = 532

Background

to me this has been my nemesis for quite a while!

Countdown is a tv quiz game (in the UK) which tests grammar and numeracy skills

Rules of the game are here

http://datagenetics.com/blog/august32014/index.html

I have tried a few times to get my head around the logic needed but i am finally getting nearer

at the moment its by no means a pretty script but in further iterations (after its fully working, i will improve my code)

Overview

using itertools https://www.geeksforgeeks.org/permutation-and-combination-in-python/ to combine the combinations of char and numbers which i will throw into my caculation machine

Functions

using reverse polish notation as the caculator

https://danishmujeeb.com/blog/2014/12/parsing-reverse-polish-notation-in-python/

http://www-stone.ch.cam.ac.uk/documentation/rrf/rpn.html

def reversepolishnotation(input1): pick some number for us(based on memory items passed)

def pick_numbers(smalln,largen): work out how near we are and if target hit send a return code of 1 back

def hownear(target,answer,formula):

Argparse

my first real go at using argpase written up at General#usingpassedarguments

g4 countdown]$ ./fixing.py -h
usage: fixing.py [-h] [-n N] [-t T] [-v V]

optional arguments:
  -h, --help  show this help message and exit
  -n N        Hardcode numbers needs to be seperated by commas i.e -n
          1,3,5,7,25,50
  -t T        Hardcode target i.e -t 300
  -v V        Verbose output 1 is on? i.e -v 1

& its quite cool

Main

at the beginning you will get a nice summary

print ("Your target is ",target_to_hit)
print ("Our large number(s) are ",our_large_numbers)
print ("Our small number(s) are ",our_small_numbers)
numbers_to_use really mean combination =2 really means x+y , 3 mean x+y+z

the digits are numbers to use -1

this helps us try 2/3/4/5 and 6 numbers combinations

numbers_to_use=2

while numbers_to_use < 7 : i have had to combine the symbols into a list because for some reason a nested loop was exiting after 1 iteration

to compound the nasty code i am also using a lot of try: except to deal with say if there are only 2 symbols to loop through

some kind of clever loop would make more sense

symbol1 = combinations_with_replacement(["+", "*", "-"], comb_needed_m1) so we have the symbols and we now grab the numbers and create the "expression" which is passed to memory

we then check the result and do a bit of cleaning up

    result = reversepolishnotation(expression)
    print (expression,result)
    rv=hownear(target=target_to_hit,answer=result,formula=expression)
    if rv==1:
        print ("Target hit")
        breakpoint()
        break
if rv==1:
    break
else:
    numbers_to_use+=1

finally we print out a summary for the user

print ("\nSummary")
outby=abs(target_to_hit-nearest_answer)
print ("We were %s out" %(outby))
 print ("We tried to hit %s the nearest we could get was %s made up of %s" %(target_to_hit,nearest_answer,nearest_solution)

Test your results

http://www.maths-resources.com/countdown/

Full run

Your target is  369
Our large number(s) are  ['100', '50']
Our small number(s) are  ['1', '4', '5', '6']
Woop

Summary
Yep we found the answer :)
We tried to hit 369 the nearest we could get was 369 made up of 100 1 5 50 4 + * - +
50 + 4 = 54
54 * 5 = 270
270 - 1 = 269
269 + 100 = 369

Clone this wiki locally