I think my code for ps2a is correct, but I think my code is kind of ugly. I have a feeling I should be using tuples in some fashion, but I'm just not that comfortable with them yet. Any thoughts?
a = 0 b = 0 c = 0 mcnuggets = 0 consecutive = 0 largest = 0 while consecutive < 6: if (6 * a) + (9 * b) + (20 * c) == mcnuggets: a = 0 b = 0 c = 0 mcnuggets += 1 consecutive += 1 elif (6 * a) + (9 * b) + (20 * c) < mcnuggets: while (6 * a) + (9 * b) + (20 * c) < mcnuggets: while (6 * a) + (9 * b) + (20 * c) < mcnuggets: while (6 * a) + (9 * b) + (20 * c) < mcnuggets: if (6 * a) + (9 * b) + (20 * c) != mcnuggets: c += 1 if (6 * a) + (9 * b) + (20 * c) != mcnuggets: c = 0 b += 1 if (6 * a) + (9 * b) + (20 * c) != mcnuggets: b = 0 c = 0 a += 1 if (6 * a) + (9 * b) + (20 * c) != mcnuggets: a = 0 b = 0 c = 0 largest = mcnuggets mcnuggets += 1 consecutive = 0 else: a = 0 b = 0 c = 0 mcnuggets += 1 consecutive += 1 print 'Largest number of McNuggets that cannot be bought in exact quantity: ' + str(largest)
I'm just deleted that final else section without any ill effects. I'm pretty sure there isn't any way for that section to ever get called, which is good because as I read it that section would add one to consecutive if the combination of sizes totaled more than mcnuggets.
Ahh, good call. And here I was thinking that the top if section was actually superfluous, but that's the section the code is actually hitting. Is it good practice, though, to use that many nested while statements? It just seems inefficient and ugly to me. Perhaps I should have used for, in somehow? I think I'm having trouble getting my head around that particular concept.
I'm almost entirely self-taught, so I don't tend to comment on what 'the right way' is because I don't know. I do know that there's another way to do that problem that uses subtraction instead of multiplication to test, which seems intuitively faster. It works like this: for smalls in range 0 to the highest possible number within the current mcnuggets for mediums in range 0 to the highest possible number given the total AND the current number of mcnuggets in the smalls larges = the remainder/number of mcnuggets in the large size check if the current combination of sizes equals the correct amount.
Thanks. I don't know if I would have come up with that logic on my own. I think I'll give it a try, though, like that. I just need to go back and brush up on my "for in range" logic first. Should be good practice. Thanks again!
OK, here's what I've come up with so far. consecutive = 0 mcnuggets = 0 largest = 0 while consecutive < 6: for smalls in range(6 * smalls <= mcnuggets): for mediums in range((6 * smalls) + (9 * mediums) <= mcnuggets): larges = (mcnuggets - ((6 * smalls) + (9 * mediums)) / 20 if (6 * smalls) + (9 * mediums) + (20 * larges) == mcnuggets: mcnuggets += 1 consecutive += 1 else: largest = mcnuggets mcnuggets += 1 consecutive = 0 print 'Largest number of McNuggets that cannot be bought in exact quantity: ' + str(largest) It's not working. I get a syntax error on the colon of my if statement to check the current combination, but that doesn't make sense to me, and I can't quite figure out where I went wrong. I'm thinking I did something wrong with my conditionals for ranges as well, but I'm not certain.
the syntax error is because you didn't yet close all the open parens on the larges = ... line above the if statement. Sometimes when that sort of thing happens the syntax error shows up in weird places. I do agree that your ranges are going to do funny things, and I have no idea what they'll do. How about for smalls in range(0, mcnuggets/6) for mediums in range (0, mcnuggets - (smalls*6))
sorry, mediums should be for mediums in range (0, (mcnuggets - (smalls*6))/9)
I think I'm going to have to come back to this one. Still having some issues: Not sure where to put the chained conditionals - my for statements don't seem to set the smalls, mediums, and larges where they need to be in order to pass validation later on. I think just stepping away for a while may help.
Join our real-time social learning platform and learn together with your friends!