This is one buggy code, and I've just realized it. http://dpaste.com/569905/ Any suggestions?
are you trying to solve 3x + 6y + 9z = n? write down on paper how you would figure it out 'by hand' then use that as a starting point for your function.
I did. 9 and 6 can both be divided by 3. So I thought I would just deduct 20 from n until it reaches an amount that can be divided by 3. From there, I simply try and see whether it can be further divided by 6. If not, int-divide it by 9 to get the value of z, and then from the remainder, divide it by 6. And actually, it does work with the problem. Every n offered by the problem set yielded the correct result. But the problem lies with smaller numbers, like 21.
That's a pretty cool way of writing it. Two possible solutions... 1) Add a qualification "while n>21: [your code here] while n<21 [code for under nugget values under 21] 2) Change the way you solve the problem. How can you solve the problem for all values both above and under 21? My code effectively ran through all the possible combinations of ax + by + cz = n using a similar while + if/elif/else combination as you did, (where a,b,c were box sizes). Not quite as elegant as your solution, but another possibility.
Thanks, Green Tiger. I would prefer to do it with your solution 2), but unfortunately, I can't think of another way, at least not atm. Doing it like no 1 could become a bad habit later. Is it possible. Is it possible to do this with the standard substitution/elimination? If so, I would try it.
Join our real-time social learning platform and learn together with your friends!