anonymous
  • anonymous
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?
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
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)
anonymous
  • anonymous
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.
anonymous
  • anonymous
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.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
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.
anonymous
  • anonymous
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!
anonymous
  • anonymous
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.
anonymous
  • anonymous
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))
anonymous
  • anonymous
sorry, mediums should be for mediums in range (0, (mcnuggets - (smalls*6))/9)
anonymous
  • anonymous
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.

Looking for something else?

Not the answer you are looking for? Search for more explanations.