elz: (ada-tubes)
elz ([personal profile] elz) wrote in [community profile] intro_to_cs2009-11-16 09:41 pm
Entry tags:

Problem Set 2

Post and discuss your answers in the comments! (And yay for everybody who posted problem set 1 answers!)

Re: Problem #4

(Anonymous) 2010-03-03 03:59 pm (UTC)(link)
ELZ - My script wound up being very similar to yours. I couldn't figure out how to stop looking after 6 consecutive successful results, so I just put all successful results in a list. I then looked for numbers not in that list and used the highest number that was missing. I'm not sure if I'm happy to find this thread AFTER I finally figured it out (hours), or if I'm glad I didn't because I think I learned more.....

## This program looks for the maximum number that can't be
## created using combinations of package sizes.


# variables - change to manipulate how what numbers to try and max range
# to try within. If the result is close to your max range, you should
# probably increase the range.
packages = [6,9,20] # variable that contains package sizes
n = range(0, 201) # range to test within. Max number must be at least TWO more than expected result.

# tests all possible combinations. Saves numbers that were successfully computed to numworks.
numworks = []
for count in n:
for a in range(0, count/packages[0]+1): #instead of using all combinations, only combinations that
# wouldn't be higher than max range (so max range divided by package size). Adding one because
# of remainders. This cuts WAY down on the time to process the program vs just using "range (0, count)"
for b in range(0, count/packages[1]+1):
for c in range(0, count/packages[2]+1):
if ((a*packages[0] + b*packages[1] + c*packages[2]) == count):
numworks = numworks + [count]

### found that entire section is not needed to get correct results. It makes numworks look nicer, but isn't needed.
### Sort then Remove duplicates from numworks
##cycle = 0
##if numworks:
## numworks.sort()
## last = numworks[-1]
## for i in range(len(numworks)-2, -1, -1):
## if last == numworks[i]:
## del numworks[i]
## else:
## last = numworks[i]

# create numbad which is list of numbers that didn't work. This is based on numbers that are not in numworks.
numbad = [0]
for ii in range (0,numworks[-1]):
if ii not in numworks:
numbad = numbad + [ii]

# prints results. Also accounts for no results.
if numbad == [0]:
print "No numbers were generated. \nYou likely used a 1 (one) or a negitive number as a package size."
else:
print "Given package sizes %i, %i, and %i, the largest number of McNuggets that \ncannot be bought in exact quantity is: %i. \n \n*** Note: the highest number tested was %i ***" % (packages[0], packages[1], packages[2], numbad[-1], n[-1])