Advertisement
Jimtuv

Chicken McNugget quantity test

Jan 31st, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. #Program to see what is the largest number that can not be derived from when ordering Chicken McNuggets in
  2. #6pc, 9pc, 20pc sizes. This checks every possible case and develops a list of possible combinations
  3. #then examines what numbers are not on that list to find the largest number you can't buy.
  4.  
  5. #James Tuvell
  6. #Feb-1-12
  7.  
  8.  
  9. bestSoFar = 0
  10. packages = (6,9,20)
  11. endtest = 150
  12.  
  13.  
  14. listcombos = []
  15.  
  16. #checkChicken
  17. #input numb is the number to be checked
  18. #a,b,c are the 3 possible piece counts a=6pc b=9pc c=20pc
  19. #output None
  20. #checks weather a combination is possible.
  21. def checkChicken (numb,a,b,c):
  22.    
  23.     combine=(packages[0]*a)+(packages[1]*b)+(packages[2]*c)
  24.     if combine == numb:
  25.         print (numb, "is a combination of ", a, b, c, " times ", packages[0], packages[1], packages[2])                
  26.         if numb not in listcombos:
  27.             listcombos.append(numb)
  28.     return
  29.  
  30. #main routine
  31. #Not much here. I chose a nested while loop and reduced the number of iterations by
  32. #dividing the test size by the piece count giving the maximum number of pieces that
  33. #could be giving in that size. This reduces the load considerably.
  34. i = 1
  35. while i <= endtest:
  36.     a = 0
  37.     while a <= endtest/packages[0]:            
  38.         b = 0
  39.         while b <= endtest/packages[1]:                    
  40.             c=0
  41.             while c <= endtest/packages[2]:            
  42.                 checkChicken(i,a,b,c)                  
  43.                 c=c+1          
  44.             b=b+1      
  45.         a=a+1  
  46.     i=i+1
  47.  
  48. print ("finished")
  49. print ("The list of possibilities up to ",i-1, "that can be bought are ", listcombos)  
  50.  
  51. #this is the section that checks for missing number on the sucess list. That will show
  52. #which ones were failures and what is the largest of them.
  53. for q in range (1, endtest):
  54.     if q not in listcombos:
  55.         bestSoFar=q
  56.         print (bestSoFar, "is not in the list")
  57.        
  58. print "The highest number you can not make with 6pc 9pc and 20pc mcnuggets is:",bestSoFar
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement