Guest User

Problem Set 2 #1

a guest
Feb 29th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. # Variable definitions:
  2. # a = # of 6 McNugget packages
  3. # b = # of 9 McNugget packages
  4. # c = # of 20 McNugget packages
  5. # n = test # of McNuggets to be purchased
  6. # successful_n = a list of all the "n" values that can be purchased
  7. # bestSoFar = the highest number that cannot be purchased
  8.  
  9. #Initialize variables
  10. successful_n = [0]
  11. bestSoFar = 0
  12. n = 1
  13. a = 0
  14.  
  15. #Start loops to test all possible combinations of a, b and c
  16. #Store successful 'n's in successful_n and failed numbers in bestSoFar
  17. while successful_n[-1] - bestSoFar < 6:
  18. while a <= n/6:
  19. b = 0
  20. while b <= n/9:
  21. c = 0
  22. while c <= n/20:
  23. result = 6*a + 9*b + 20*c
  24. if result == n:
  25. if successful_n.count(n) == 0:
  26. successful_n.append(n)
  27. c = c + 1
  28. b = b + 1
  29. a = a + 1
  30. if successful_n.count(n) == 0:
  31. bestSoFar = n
  32. n = n + 1
  33. a = 0
  34.  
  35. ##print successful_n #used in testing phase
  36. ##print bestSoFar #used in testing phase
  37. print """
  38. Given package sizes of 6, 9 and 20, the largest quantity of McNuggets
  39. that cannot be purchased in an exact quantity is:
  40. """
  41. print bestSoFar
Advertisement
Add Comment
Please, Sign In to add comment