looping through possible combinations of McNuggets packs of 6, 9 and 20
Peter Otten
__peter__ at web.de
Fri Aug 13 11:46:03 CEST 2010
Martin P. Hellwig wrote:
> SPOILER ALTER: THIS POST CONTAINS A POSSIBLE SOLUTION
>
> On 08/12/10 21:41, News123 wrote:
>
>> On 08/12/2010 09:56 PM, Martin P. Hellwig wrote:
>>> On 08/11/10 21:14, Baba wrote:
>>> <cut>
>>>
>>> How about rephrasing that question in your mind first, i.e.:
>>>
>>> For every number that is one higher then the previous one*:
>>> If this number is dividable by:
>>> 6 or 9 or 20 or any combination of 6, 9, 20
>>> than this number _can_ be bought in an exact number
>>> else
>>> print this number
>>>
>>
>> you are allowed to mix.
>> 15 is neither divisable by 6 nor by nine, but 9 + 6 = 15
>
> I was aware of that, thats whhy I wrote:
> "or any combination of 6, 9, 20"
>
>>
>> I guess, trying to find the result with divisions and remainders is
>> overly complicated.
>
> Python 2.6.4 (r264:75706, Jul 1 2010, 12:52:41)
> [GCC 4.2.1 20070719 [FreeBSD]] on freebsd8
> Type "help", "copyright", "credits" or "license" for more information.
> >>> MODULO_COMBINATIONS = [[20], [9], [6],
> ... [20, 9], [20, 6], [9, 20],
> ... [9, 6], [6, 20], [6, 9],
> ... [20, 9, 6], [20, 6, 9], [9, 20, 6],
> ... [9, 6, 20], [6, 20, 9], [6, 9, 20]]
> >>>
> >>> def apply_combinations_on(number):
> ... tmp = list()
> ... for combination in MODULO_COMBINATIONS:
> ... remainder = number
> ... for modulo_value in combination:
> ... if remainder == 0:
> ... remainder = None
> ... break
> ...
> ... result = remainder % modulo_value
> ...
> ... if result == remainder :
> ... remainder = None
> ... break
> ...
> ... remainder = result
> ...
> ... if remainder == 0:
> ... tmp.append(str(combination))
> ... return(tmp)
> ...
> >>> print(apply_combinations_on(15))
> ['[9, 6]']
> >>>
>
> What is so over complicated about it?
>
Well, it was hard enough for me to run the code rather than just read it.
I get
>>> apply_combinations_on(21)
[]
which should be 1*9 + 2*6
What am I missing?
Peter
More information about the Python-list
mailing list