number generator

Shane Geiger sgeiger at ncee.net
Wed Mar 14 16:35:02 CET 2007


Raymond:   It looks to me as if you are trying to turn a generator into 
a list in the final line.  That doesn't work.



Raymond Hettinger wrote:
>> To make the solutions equi-probable, a simple approach is to
>> recursively enumerate all possibilities and then choose one of them
>> with random.choice().
>>     
>
> Since people are posting their solutions now (originally only hints
> were provided for the homework problem), here's mine:
>
> def genpool(n, m):
>     if n == 1:
>         yield [m]
>     else:
>         for i in xrange(1, m):
>             for rest in genpool(n-1, m-i):
>                 yield rest + [i]
>
> import random
> print random.choice(list(genpool(n=4, m=20)))
>
>   

-- 
Shane Geiger
IT Director
National Council on Economic Education
sgeiger at ncee.net  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: sgeiger.vcf
Type: text/x-vcard
Size: 310 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20070314/da4d8083/attachment.vcf>


More information about the Python-list mailing list