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

Roald de Vries downaold at gmail.com
Fri Aug 13 12:25:06 CEST 2010


On Aug 12, 2010, at 10:51 PM, John Posner wrote:
> On 8/12/2010 9:22 AM, Dave Angel wrote:
>>>
>>> Now you have to find the largest number below 120, which you can
>>> easily do with brute force
>    tgt = 120 # thanks, Dave Angel

Anytime, but I'm not Dave Angel.

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]

Cheers, Roald



More information about the Python-list mailing list