looping through possible combinations of McNuggets packs of 6, 9 and 20

News123 news1234 at free.fr
Fri Aug 13 16:22:44 EDT 2010


Roald,


What would your solution be if you weren't allowed to 'know'
that 120 is an upper limit.

Assume you were only allowed to 'know', that you won't find
any other amount, which can't be bought A AOON A
you found six solutions in a row?

I have a rather straightforward solution trying  from 0 nuggets on
until I found six 'hits' in a row, but would be interested about other
idioms.


On 08/13/2010 12:38 PM, Roald de Vries wrote:
> On Aug 13, 2010, at 12:25 PM, Roald de Vries wrote:
>> My previous algorithm was more efficient, but for those who like
>> one-liners:
>>
>> [x for x in range(120) if any(20*a+9*b+6*c == x for a in range(x/20)
>> for b in range(x/9) for c in range(x/6))][-1]
> 
> OK, I did some real testing now, and there's some small error in the
> above. All solutions for all x's are given by:
> 
>     [(x, a, b, c) for x in range(120) for a in range(x/20+1) for b in
> range(x/9+1) for c in range(x/6+1) if x == a*20+b*9+c*6]
> 
> ... and all non-solutions by:
> 
>     [x for x in range(120) if not any(x == a*20+b*9+c*6 for a in
> range(x/20+1) for b in range(x/9+1) for c in range(x/6+1))]
> 
> Cheers, Roald




More information about the Python-list mailing list