Topic: MIT OCW PROBLEM SET SERIES 4/n
I have found really interesting the courses about Python at
I have decided to submit my solutions to the suggested problems. I do not think mine the best solutions possible, here i share them with you with the hope to have usefull feedback.
You can find the text of the problem in the site that I suggested, i will not repeat it here.
Here is the code for ps2b.
def largestNumberUnpurchasableMcNuggets(pack,limit):
"""Largest number of McNuggets that cannot be bought in exact quantity"""
def mcNuggets(requested,pack,limit):
"""Complete search"""
for q1 in range(0,limit/pack[0]+1):
for q2 in range(0,limit/pack[1]+1):
for q3 in range(0,limit/pack[2]+1):
if pack[0]*q1+pack[1]*q2+pack[2]*q3 == requested:
return (q1,q2,q3)
assert(False)
n = limit
while (n > pack[0]-1):
try:
quantities = mcNuggets(n,pack,limit)
n -= 1
except:
assert(n<limit)
return n
return pack[0]-1
def problemSet2():
"""McNuggets Diophantine equation"""
pack = (6, 9, 20)
limit = 200
print "Given package sizes",pack,"the largest number of McNuggets that cannot be bought in exact quantity is:",largestNumberUnpurchasableMcNuggets(pack, limit)
pack = (7, 13, 20)
limit = 200
print "Given package sizes",pack,"the largest number of McNuggets that cannot be bought in exact quantity is:",largestNumberUnpurchasableMcNuggets(pack, limit)
pack = (9, 17, 20)
limit = 200
print "Given package sizes",pack,"the largest number of McNuggets that cannot be bought in exact quantity is:",largestNumberUnpurchasableMcNuggets(pack, limit)Last edited by 5tarson (February 5, 2012 15:22)