alright... I could use some more help. For problem set 2 (first question): I think I'm having trouble coming up with the right process: 1. First I'm testing if 6, 9, or 20 alone go evenly into a given number of nuggets 2. then I'm testing if 6, 9, or 20...when any two are added...equals the given number of nuggets 3. then I'm keeping two variables static and testing multiples of one (ie 6*4+9*1+20*1) 3. then I'm keeping one variable static and testing paired multiples of the other two (ie 6*1+9*4+20*4) http://codepad.org/UmurtfNo
Now I feel I'm at where I need to check solutions to 'equation' when the variables a,b,c are changing differently but I can't wrap my head around how to do that. I get glimpses (from about 20,000feet above) of how to do that but when i start writing things down I get spastic and loose my train of thought. Any hints?
I'm almost thinking that mathematically there's a better way to do this?
Albeit you are thinking correctly in the sense that you should fix a variable and then test other variables via brute force, I think that there is a better way to code it: you can use nested for loops. For instance, you can for i in range(n): for j in range(n): for k in range(n): //code. What this does is that it initializes the i variable as 0, then initializes j as 0 and then loops through the innermost loop holding i = j = 0, and then it goes for j=1, and loops the innermost loop again, etc. This is far for efficient, and there are blatantly obvious ways to improve this code, like checking for divisibility with 6, 9 and 20, or not checking until n (you can stop before, how soon is a good question), or using that identity described in the problem set .pdf, but it gives you a safe haven, if you like, to start your coding after it. What is most important is, like me and dmancine wrote somewhere else, tackling a smaller version of the problem, checking if, given a value, you can buy it with these size of packages.
you are trying to see if there is a combination of 6, 9, and twenty packs that can buy n nuggets. One easy way to do that is to check Every combination of 6, 9, and 20 packs. Nested loops can 'generate' those combinations for you so you can test them. A good analogy for 3 nested loops is the 10's, 1's and tenth's wheels (digits) on your car's odometer. The tenths wheel is the innermost loop and the 10's wheel is the outer loop. http://ideone.com/aoPUh
That is a huge help! With some slight modification the nested loops are working for me. I need to think more about what's going on with it, why it's working, and why I missed something so simple in the beginning. I was trying to build something that would test all possible combinations but with individual loops, not with them nested. Guess I don't have my brain wrapped around how/when they work yet. Well on one hand ti's good to know that it isn't necessarily the math that is getting me as much as it is my lack of understanding about the code...
here's my modification: http://codepad.org/m76NpUv8 Can someone describe what is happening in this code. I just want to make sure I have it right. Is it first finding 20's in the range and storing them somewhere, then finding 9's and storing, then finding 6's and storing, then it checks to see if any add up to n? The reason I am getting combinations with 0 in them (ie 0 1 1) is because the range I've chosen INCLUDES/starts at 0? I suppose what surprises me and what I wouldn't expect to happen is for Python to know to check combinations of the same number...I would think, looking at the steps, that Python would check one nTwenties with one nNines with one nSixes (or 0 with 9 and 6, or 20 with 0 and 6, etc) NOT that it would check 1 nTwenties+4 nNines+1 nSixes... The 4 nNines surprises me. That's definitely one to highlight in my notes. I guess I need to think about how Python stores values and then looks at them afterward...
As far as what is happening and when it is happening, the first loop to run through its entire range once will be the innermost loop, but it is actually the outermost loop that begins first. bwCA's odometer explanation with the accompanying visual is a good explanation, but I will attempt to walk you through one go through your nested loop: First the interpreter sees the for loop with the variable nTwenties and sets nTwenties to 0. Then it starts to interpret the code inside that loop. The next line in this case is another for loop. Now nNines is set to 0 and the code inside that for loop is interpreted. This starts the for loop with nSixes which begins at 0 as well. Now this innermost loop will run until it goes through the range you choose. Since the innermost loop is now finished, the interpreter returns to your second for loop which is now set to its second number in the range and then the innermost loop will again go through its range. As I said, if you look above at bwCA's code and follow what it happening with the numbers printed, you should be able to follow what is happening with the loops. Beyond that I have two suggestions for you. First, I think you were on the right track with your original equation: (6*a)+(9*b)+(20*c). This should be somewhere in your code, because it is the equation for which you utlimately want to try to find solutions. Second, look again at the range() function. The range function is structured in this way: range(<start>, <stop>, <step>). Thus, <start> will be the first number substituted into, for example, nSixes in your inner loop. The loop will run for 1 fewer times than <stop>. Finally, <step> is an optional argument, and it sets the interval for the loop. If no argument is supplied for <step>, the default is that the loop will count by ones (e.g., if <start> is set to 0 and <step> is left blank, nSixes will start at 0. The second time through the loop, nSixes will be 1 and so on). If you supply your own argument, nSixes will increase by that number each time. In your code as is, nSixes begins at 0 and will increase by 6 each time through the loop. I hope this helps.
cb12 thanks for taking the time to write that. I went back and looked at what the code was putting out as well and it all makes perfect sense. I've spent so much time on this problem set and I've finally got it all done... Code is working great for all the questions in the problem and I'm so friggin' happy. What I ended up figuring out is that I account for the variables of nugget packs in the 'step' for each range. I've literally been working on all this for about 2.5 days. Here's to hoping I get more efficient.
...yeah I see what you did now...my bad
Join our real-time social learning platform and learn together with your friends!