looping through possible combinations of McNuggets packs of 6,9 and 20
MRAB
python at mrabarnett.plus.com
Fri Aug 13 13:57:24 EDT 2010
Martin P. Hellwig wrote:
> On 08/13/10 10:46, Peter Otten wrote:
>> Martin P. Hellwig wrote:
>>
>>> SPOILER ALTER: THIS POST CONTAINS A POSSIBLE SOLUTION
> No it wasn't :-)
>
>> which should be 1*9 + 2*6
>>
>> What am I missing?
>>
>
> Aah interesting, 21 % 9 returns 3 instead of 12, which makes sense of
> course. I guess the algorithm has to be adapted in a way that if the
> value is bigger or equal twice the size of the modulo value you need to
> iterate over it, something like:
> for minus_multiplier in range(1, int(number, modulo_value)+2):
> number = number - (modulo_value * minus_multiplier)
> do the rest of the loop
>
> Probably another ten lines or so to make it working as it should
>
I don't understand what you're trying to do. My solution would be:
def can_buy(nuggets):
for packs_20 in range(nuggets // 20, -1, -1):
remaining_20 = nuggets - 20 * packs_20
for packs_9 in range(remaining_20 // 9, -1, -1):
remaining_9 = remaining_20 - 9 * packs_9
if remaining_9 % 6 == 0:
packs_6 = remaining_9 // 6
return [packs_6, packs_9, packs_20]
return []
More information about the Python-list
mailing list