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):
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()
To find the max number of times you can include pack2 in target, you can just use: target/pack2 I attached my ps2a.py for reference if you're interested.
Join our real-time social learning platform and learn together with your friends!