looping through possible combinations of McNuggets packs of 6, 9 and 20
Roald de Vries
downaold at gmail.com
Fri Aug 13 06:38:53 EDT 2010
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