Just for fun: Countdown numbers game solver

Arnaud Delobelle arnodel at googlemail.com
Mon Jan 28 23:11:09 CET 2008


On Jan 28, 9:31 pm, david.hot... at blueyonder.co.uk wrote:
> I also had a go at this problem for a bit of python practice, about 6
> months ago.  I tried a few optimizations and my experience was that
> with only 6 seeds, a hash table was very effective.  So effective, in
> fact, that it made all other optimizations more or less pointless.

My strategy was to walk through each solution "only once" (for a
certain definition of "only once :), thus I was hoping not to need a
hashtable.

> Code below.  Arguments are seeds first, then targets.  Like this:
>
> C:\utils>countdown.py 7 8 50 8 1 3 923
> made target!
> 50 * 8 = 400
> 400 - 1 = 399
> 399 / 3 = 133
> 133 * 7 = 931
> 931 - 8 = 923
>
> Took 0.421 seconds

Did you pick these numbers at random?  Interestingly, the solution is
unique:

[0.27424502372741699]
>>> list(test(nums=[7, 8, 50, 8, 1, 3], target=923, method=divide, countdown=print_countdown)) (50*8-1)*7/3-8
(50*8-1)*7/3-8
[0.2748568058013916]

To find just one solution:

>>> list(test(nums=[7, 8, 50, 8, 1, 3], target=923, countdown=first_countdown))          [0.11860203742980957]

(list returned contains time taken to reach solution.  By comparison
your code takes 0.264s on my machine.  Mine is faster on this example,
but one example is not representative!)

--
Arnaud



More information about the Python-list mailing list