Quantcast

A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Syamantak

  • 2 years ago

Write a function, called numPens that takes one argument, n, and returns True if it is possible to buy a combination of 5, 8 and 24 pack unit in Python.

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

    How much python do you know? Do you know how to make a function? Do you have any code?

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

    Yes I know how to make a function.

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

    This is the exact question def numPens(n): """ n is a non-negative integer Returns True if some non-negative integer combination of 5, 8 and 24 equals n Otherwise returns False. """

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

    @Syamantak Are you still there?

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

    Yes

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

    I think i might have a solution, but I to be honest with you I have more experience with Java, so i might not be the most optimal one.

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

    Sorry the solution is not right , but nevertheless Thank You !

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

    Do you have some kind of answer to compare against?

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

    No there is no answer to compare against

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

    Do we have to use all three of them or could we use combinations with only one or two of the diffrent packsizes? for example: n = 15 => 3*5 pack

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

    We could use combination of any one any two or all three , we could also take a number twice and a number once

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

    for example a=5,b=8 and c=24 and n can be anything.It must be done like a+b or 2(A+B) OR A+B+C

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

    This is basically a variation of the old McNugget Problem. I am sure you can find lots on this on google. Here is a link to some related math, hope it helps http://www.mathnerds.com/best/mcnuggets/index.aspx

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

    And here is McNuggets in Python, found on google. def McNuggets(n): if n == 0: return True for i in (6, 9, 20): if n >= i and McNuggets(n - i): return True return False

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

    Slight adaption of my other post for fun. def McNuggets(sizes,n): if n == 0: return True for i in (sizes): if n >= i and McNuggets(sizes,n - i): return True return False for i in range(50): print(i,McNuggets((6,9,20),i)) for i in range(50): print(i,McNuggets((5,8,24),i))

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

    Lyrae, the only difference in output between the two seems to be at 0, mine shows True and yours shows False. Not sure which would be considered more accurate.

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

    It's probably yours, mine seem to fail at higer values(?) And also, recursive functions are awesome ;)

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

    Thinking about it, is 0 even considered a possitive integer(?)

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

    I would think no. Something interesting about the second function I posted is you can send it any set of sizes like below with 4 sizes without changing the function.. for i in range(50): print(i,McNuggets((6,9,25,31),i))

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

    Here're 3 different implementations of the same problem and they all preform equal. Like @msmithhnova said it's probably a slight modification on the McNuggets Problem. def numPens(n): i = 0 while i <= n: j = i; while j <= n: k = j while k <= n: if k == n: return True k += 24 j += 8 i += 5 return False def numPens(n): if n == 0: return True for i in (5, 8, 24): if n >= i and numPens(n - i): return True return False def numPens(n): if n == 0: return True elif n < 0: return False elif numPens(n - 24) or numPens(n - 8) or numPens(n - 5): return True else: return False

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