A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 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?

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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)

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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!

  6. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  7. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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))

  8. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    sorry, mediums should be for mediums in range (0, (mcnuggets - (smalls*6))/9)

  9. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.