• anonymous
I can get PS2 working, but solution is clumsy. Any suggestions on improvements? The snippet: int(math.ceil(target*1.0/pack2))+1 tries to find the max number of times we can include a package of size pack2, without exceeding the target. packages = (6,9,20) pack1 = packages[0] pack2 = packages[1] pack3 = packages[2] inattain = [] consec_attain = 0 for target in range(1,200): attain = False for ii in range(0,int(math.ceil(target*1.0/pack1))+1): for jj in range(0,int(math.ceil(target*1.0/pack2))+1): for kk in range(0,int(math.ceil(target*1.0/pack3))+1):
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
  • jamiebookeater
I got my questions answered at in under 10 minutes. Go to now for free help!
  • anonymous
if pack1*ii+pack2*jj+pack3*kk == target: #We can get it attain = True #End exhaustive search if not attain: inattain.append(target) consec_attain = 0 else: consec_attain += 1 if consec_attain == min(packages): break #End outer for print "Given package sizes, largest number that cannot be bought is: ", inattain.pop()
  • anonymous
To find the max number of times you can include pack2 in target, you can just use: target/pack2 I attached my for reference if you're interested.
1 Attachment

Looking for something else?

Not the answer you are looking for? Search for more explanations.