A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
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?
anonymous
 5 years ago
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?

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0a = 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
 5 years ago
Best ResponseYou've already chosen the best response.0I'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
 5 years ago
Best ResponseYou've already chosen the best response.0Ahh, 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I'm almost entirely selftaught, 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
 5 years ago
Best ResponseYou've already chosen the best response.0Thanks. 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
 5 years ago
Best ResponseYou've already chosen the best response.0OK, 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
 5 years ago
Best ResponseYou've already chosen the best response.0the 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
 5 years ago
Best ResponseYou've already chosen the best response.0sorry, mediums should be for mediums in range (0, (mcnuggets  (smalls*6))/9)

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I 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.
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.