recursive algorithm for balls in numbered boxes

Dr. Phillip M. Feldman Phillip.M.Feldman at gmail.com
Sun Sep 11 21:41:33 CEST 2011


Hello Peter,

When I run my code, I get the same 14 configurations that your code
produces; the only different that I can see in the output is that the
configurations are produced in a different order.  Note that your code is
not creating an iterator, so thus doesn't do what I want.  Also, generating
the product set and then testing whether the total number of balls is
correct will potentially consider a huge number of cases that must be
rejected because the sum is wrong; this is too inefficient.

Phillip

In [2]: list(balls_in_numbered_boxes(10,[4,3,2,1,2]))
Out[2]:
[(4, 3, 2, 1, 0),
 (4, 3, 2, 0, 1),
 (4, 3, 1, 1, 1),
 (4, 3, 1, 0, 2),
 (4, 3, 0, 1, 2),
 (4, 2, 2, 1, 1),
 (4, 2, 2, 0, 2),
 (4, 2, 1, 1, 2),
 (4, 1, 2, 1, 2),
 (3, 3, 2, 1, 1),
 (3, 3, 2, 0, 2),
 (3, 3, 1, 1, 2),
 (3, 2, 2, 1, 2),
 (2, 3, 2, 1, 2)]
-- 
View this message in context: http://old.nabble.com/recursive-algorithm-for-balls-in-numbered-boxes-tp32440187p32443548.html
Sent from the Python - python-list mailing list archive at Nabble.com.




More information about the Python-list mailing list