looping through possible combinations of McNuggets packs of 6, 9 and 20
Baba
raoulbia at gmail.com
Fri Aug 13 14:25:47 EDT 2010
Hi News 123,
Ok i'm getting closer. I am able to write code that will output values
that can be bought in exact quantity (truelist) and values that cannot
be bought in exact quantities.
For a range up to 29 i get this:
true [6, 9, 12, 15, 18, 20, 21, 24, 26, 27, 29]
false [0, 1, 2, 3, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 22, 23, 25,
28]
the sixth value that passes the test of having an exact solution is 20
so that would mean that the last number i got that cannot be bought in
exact quantity is 19
that doesn't seem quite right, does it?
def can_buy(n_nuggets):
for a in range (0,n_nuggets):
for b in range (0,n_nuggets):
for c in range (0,n_nuggets):
#print "trying for %d: %d %d %d" % (n_nuggets,a,b,c)
if 6*a+9*b+20*c==n_nuggets:
return True
return False
truelist=[]
falselist=[]
for n_nuggets in range(30):
result = can_buy(n_nuggets)
if result==True:
truelist=truelist+[n_nuggets,]
else:
falselist=falselist+[n_nuggets,]
print 'true',truelist
print 'false',falselist
tnx
Baba
More information about the Python-list
mailing list