working on problem set 2, problem 3. can somebody please tell me why my variable bestSoFar doesn't seem to be updating at all? bestSoFar = 0 # variable that keeps track of largest number # of McNuggets that cannot be bought in exact quantity twentyPack = 0 ninePack = 0 sixPack = 0 for n in range(1, 60): # only search for solutions up to size 150 print 'n = ', n while twentyPack<= 60: while ninePack <=60: while sixPack <=60: if (6*sixPack) + (9*ninePack) + (20*twentyPack) == n: bestSoFar = n
cut off my code sorry.
Add some print statements.
tried that. the loops seem to be running fine until it comes to testing and assigning 'n'
I started my test loop with the test '(6*x) + (9*y) + (20*z) != n' and itterate through combos inside.
i think your nested while loops is the issue. always more than one way to code, so not definate here. let me play with yours some more.
yeah i like the idea of testing before running the loops. you're using an 'if' statement, i assume?
first thing is your range of 60. arbritrary here. one of the points of the lesson was the diaphantine equation. using this, you know that once you find 5 matches in a row, you know every int following will work. so init a variable to count the matches you get and one to track the last known fail. when you get 5 matches in a row the last fail number is your answer.
actually, i us a while statement, testing the formula (6x+9y+20z) != testNum. when test fails, I know test_num is not a match, so then itterate the x, y, z variables. if get through all the x,y,z combos I know test_num is not a match. reset x,y,z itterate testNum by 1 and loop again.
to be clear. when I say "when test fails" i mean the while test is True. Meaning 6x+9y+20z does NOT equal testNum so you run whats IN the loop. sorry, confusing.
print statements are good - look at the output from this, it should be obvious http://dpaste.com/683395/
In your itteration loop. You are testing the testnumber versus all of the different packs. When testNum = 1 and you're starting with a sixpack this isn't very efficient. So what are some of things that we know to make the formula true? You must buy at least one sixpack, so the test number must be six or greater. Also you know that testing the six pack after it reaches the largest multiple of 6 in the testNum is not necessary. Another way of saying that is: if 6-pack >= test number/6 You no longer need to test for the sixpack. The same goes for the nine packs and 20 packs dividing by nine and 20, accordingly. This is how I run my iteration loop. If you want to see my code. Let me know.
i was able to finish the program by starting from scratch and going a completely different direction. thank you all for all of your help though!!!!'
alright! sometimes its best to start over, than to keep hitting your head against the wall trying to debug first try. lesson learned! :)
I know you already solved the problem in a different way but one of the problems with the code above is the logic: if (6*sixPack) + (9*ninePack) + (20*twentyPack) == n: bestSoFar = n The statement above is the opposite of what you want. If the diophantine equation is satisfied with some combination (e.g. n=35, sixPack =1, ninePack=1, twentyPack=1), then bestSoFar is not n.) What you want is the exercise is the highest number that *can't* be bought or is *not* satisfied by the equation.
Join our real-time social learning platform and learn together with your friends!