Hello. I'm working on Problem set 2, question 1 and can't seem to get what I am thinking to work. Here's what I've got so far: a = 0 b = 0 c = 0 n = int(raw_input("Please enter a number:")) for a in range(1,15): if (6*a) + (9*b) + (20*c) == n: print "You need this many six packs:", a for b in range(1,15): if (6*a) + (9*b) + (20*c) == n: print "You need this many nine packs:", b for c in range (1,10): if (6*a) + (9*b) + (20*c) == n: print "You need this many 20 packs:", c Thanks in advance!
I approached that question by writing out a list of multiples of 6, then 9, then 20 up to about 50. Then it is easy to figure out which combinations of multiples add up to the given values. This exercise leads to the next(problems 2 & 3), where you generate lists of multiples of three given "packets." Remember this lecture dealt with lists and tuples. Generate each list, then add each item in each list to every other item to get all possible values of the equation. Then check for the correct answer within this big list. If the answer is there, the equation is solved, if not, not - and if it is solved five times in a row, the theorem tells you that you need go no further. I also developed functions to sort the lists, cut out the duplicates and cut out values above 50, making the computations faster and the lists less cumbersome.
You can solve it in a way similar to what you have started, but you need to nest the for loops, also they should start at 0: for a in range (0,15): for b in range (0,15): for c in range (0,10): ...(do testing here)... It's not the most elegant, but it will work for question 1. A simple improvement would be to use integer division for the ranges e.g. range(0,n/6+1) for a, range(0,n/9+1) for b. That way you're testing a reasonable set of values. The first time I worked on the solution I did something similar to what you have and then I improved it until I was satisfied with the program and that I had learned sufficiently from the problem set. Actually, when I was trying to understand problem 1 and 2 at the very beginning I figured out the combinations that worked by hand. It was easier to think about the following questions after that.
That's the way I implemented exhaustive search, I hope it helps: """Performs exhaustive search to find the largest number of McNuggets that cannot be bought in exact quantity""" def solve(n, packages): p1 = packages[0] p2 = packages[1] p3 = packages[2] success = 0 for a in range(n / p1 + 1): if success == 1: break for b in range((n - a) / p2 + 1): if success == 1: break for c in range((n - a - b) / p3 + 1): comb = p1 * a + p2 * b + p3 * c if comb == n: success = 1 return success def test(): packages = input('Input as a list the size of the three packages: ') bestSoFar = 0 for n in range(1,150): sol = solve(n, packages) if sol != 1: bestSoFar = n print 'Given package sizes %r, %r and %r, the largest number of McNuggets that' % (packages[0], packages[1], packages[2]) print 'cannot be bought in exact quantity is: %r' % (bestSoFar) test()
bcolumbus or Mattk, I'd like to see your code when you're done b/c it's a completely different approach than mine.
mattk, thank you very much for your advice. I think I got it to work. The only question I have now is, using this same method, can you make only the first option print?. Right now the program is printing every possible solution (e.g., if n=56, there could be 3 different combinations). Here's the code: #define n as the variable number of nuggets n = int(raw_input("Please enter a number:")) print "\nBuy one of the following combinations: \n" for a in range(0,n/6+1): #loops through possibilities for a for b in range(0,n/9+1): #loops through poss. for b for c in range (0,n/20+1): #loops through poss. for c if (6*a) + (9*b) + (20*c) == n: print a, "six packs,", b, "nine packs, and", c, "20 packs." #print the combinations that work for n
Wow, mine is 3x as long as arimbr's and very clunky. What do the %s mean in arimbr's code?
@Joe Coale I don't know exactly what do you mean by %s. Try it, I give you an example: print 'My name is %s' % 'Ari' print 'I am %d years old' % 20 print 'My name is %s and I am %d years old' % ('Ari', 20) %s is expecting a string %d is expecting a decimal %r is for raw, I think it prints whatever you give, It is not expecting some specific format
Join our real-time social learning platform and learn together with your friends!