I'm working on Problem Set 2, Problem #4. I thought I had it, but it turns out there's definitely a bug. If I set 1 as the size of the smallest nugget pack, it tells me that the highest number that cannot be bought is one less than the second-highest nugget pack size. If I set the values as 2, 3, and 7, it evaluates every number as being possible. Here's what I've got: http://goo.gl/FUZAz Help would be much appreciated!
so which value of n (<200) cannot be bought with a combination of 2, 3 and 7 nugget packs?
bwCA mad a point. The reason it evaluates as all as being possible, is because they are all possible. he mentions once you reach x, x+1,...,x+5 all being possible values when the combination is 6, 9, and 20. It's because 6 is the smallest amount we can increment by. In 2, 3, and 7 you only need x, and x+1 to be true, because 2 is the smallest unit we can increment by. That means, in this case x is going to be 2 and all values > 2 are possible.
The problem you're having is with your variable 'n'. The first problem is you're walking it up from the smallest pack size with your line n = (smpack). This means your code is going to miss any number under that. The second problem is that you are using n in your numbertest function where you shouldn't, in your for loop ranges. So for very low values, you're not trying enough packs to get the total number. (eg. If your packs = [1,6,7], and you give it an n of 5, youre only trying 0-4 packs of 1 nugget and giving up and saying its impossible when of course anything is possible with nugget packs) I've attached a cleaned up version of your code which should work now and I've also cut out some stuff that save you some lines and variables (check out how I pass the True and False returns to the if statement) Hope that helps!
oops, forgot to save all the changes first, use this one instead
Join our real-time social learning platform and learn together with your friends!