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

News123 news1234 at free.fr
Wed Aug 11 16:48:26 EDT 2010


As said in the instructions.

if you find six consecutive numbers, that can be bough in exact
quantity, then you know, that all bigger numbers can also be bought in
exact quantity.


I would do a brute force approach


first I would create one function, which will try to find out, whether
one can buy an exact quantity of n nuggets.


example function prototype


def can_buy(n_nuggets):
	# here you have to write your code


the function should return True or if you're curious a list of packages
and quantities if quantity n_nuggets can be bought

otherwise it should return False or an empty list.



then you can create another function which will start with 6 nuggets (or
if you like to with 1 nugget)
and which will count how many times in sequence it managed to return a
result. (by using the function can_buy() and managibng a counter)

If it found 6 results in sequence, then you know, that all bigger
numbers can also be bought and that the biggest number, which could not
be bought was 6 numbers before.

I think nobody here will write the soultion for you.

If you write some more code and if you tell us what it's supposed to to
and with what exectly you're having trouble with I can give you more hints.




On 08/11/2010 10:14 PM, Baba wrote:
> level: beginner
> 
> exercise: given that packs of McNuggets can only be bought in 6, 9 or
> 20 packs, write an exhaustive search to find the largest number of
> McNuggets that cannot be bought in exact quantity.
> 
> exercise source:
> http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/assignments/pset2.pdf
> 
> please help me write this code
> 
> i believe it's something along the lines of this:
> 
> c=0
> sol=[]
> for n in range (0,10):
>  for a in range (0,10):
>   for b in range (0,10):
>    for c in range (0,10):
>     sol=6*a+9*b+20*c
>     if sol!=n:
>      c+=1
>     if c==6:
>      print sol


Not very modular.
> c=0 # c could have a meaningful name and probably a different  one
>     # it seems, that you reuse c also in a for statement
> sol=[]
> for n in range (0,10): # n should not only go from 0 to 10
>                       #  but from 0 until c is 6

# i'd put this in a separate function it makes it also easier for
# you to understand and test
>  for a in range (0,10):
>   for b in range (0,10):
>    for c in range (0,10): # c used here and lso as counter
>     sol=6*a+9*b+20*c
>     if sol!=n:
>      c+=1
>     if c==6:
>      print "solution is",sol-6
> 





More information about the Python-list mailing list