morfir

Finished PS2

Jan 27th, 2012
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.24 KB | None | 0 0
  1.  
  2. ##  6a + 9b + 20c = N where N = totalNugs
  3.  
  4. print "Please enter the desired number of chicken nuggets to test:"
  5. while True:
  6.     try:
  7.         totalNugs = int(raw_input())
  8.         if totalNugs > 5:
  9.             break
  10.         elif totalNugs <= 5:
  11.             print "Please enter a valid number of nuggets, 6 or more\n"
  12.     except ValueError:
  13.         print "Please enter a valid number of nuggets, 6 or more\n"
  14.  
  15. ##Globalcounter to change the flow of the program after the first execution
  16. globalCount = 0
  17. rerun = True
  18.  
  19. while rerun == True:
  20.     a = 0
  21.     b = 0
  22.     c = 0
  23.     aswap = 0
  24.     bswap = 0
  25.     cswap = 0
  26.  
  27.     ## get maximum values for a-b-c when all other variables equal 0.
  28.     ## get atop
  29.     atop = 0
  30.     while (a*6) <= totalNugs:
  31.         if (a*6) == totalNugs:
  32.             aswap = a
  33.             break
  34.         else:
  35.             a = a + 1
  36.     atop = a - 1
  37.     if aswap > atop:
  38.         atop = aswap
  39.     a = 0
  40.  
  41.     ## get btop
  42.     btop = 0
  43.     while (b*9) <= totalNugs:
  44.         if (b*9) == totalNugs:
  45.             bswap = b
  46.             break
  47.         else:
  48.             b = b + 1
  49.     btop = b - 1
  50.     if bswap > btop:
  51.         btop = bswap
  52.     b = 0
  53.  
  54.     ## get ctop
  55.     ctop = 0
  56.     while (c*20) <= totalNugs:
  57.         if (c*20) == totalNugs:
  58.             cswap = c
  59.             break
  60.         else:
  61.             c = c + 1
  62.     ctop = c - 1
  63.     if cswap > ctop:
  64.         ctop = cswap
  65.     c = 0
  66.  
  67.     ## Begin testing all possible combinations of 6(a->atop) + 9(b->btop) + 20(c->ctop) = totalNugs
  68.     ## Global count = 0, First time run code.
  69.     while globalCount == 0:
  70.         foundCombination = False
  71.         possibleCombinations = 0
  72.         for i in xrange(atop + 1):
  73.             for j in xrange(btop + 1):
  74.                 for k in xrange(ctop + 1):
  75.                     tempCheck = ((i*6) + (j*9) + (k*20))
  76.                     if tempCheck == totalNugs:
  77.                         possibleCombinations = possibleCombinations + 1
  78.                         foundCombination = True
  79.         ## Break the loop and set globalCounter = 0 to execute 1st run code below
  80.         globalCount = 2
  81.     if globalCount == 2:
  82.         globalCount = 0
  83.  
  84.     ## Second time run code, globalCounter = 1
  85.     while globalCount == 1:
  86.         foundCombination = False
  87.         possibleCombinations = 0
  88.         for i in xrange(atop + 1):
  89.             for j in xrange(btop + 1):
  90.                 for k in xrange(ctop + 1):
  91.                     tempCheck = ((i*6) + (j*9) + (k*20))
  92.                     if tempCheck == totalNugs:
  93.                         print "It is possible to get " + str(totalNugs) + " nuggets by combining"
  94.                         print str(i) + " orders of 6, " +  str(j) + " orders of 9, and " + str(k) + " orders of 20\n"
  95.                         possibleCombinations = possibleCombinations + 1
  96.                         foundCombination = True
  97.                         ## Program has completely finished now after a second time running and outputing possible combinations
  98.                         globalCount = 3
  99.     if globalCount == 3:
  100.         break
  101.     rerun == False
  102.  
  103.     ## Runs only once upon first run, and skips after second run to list all possibilities
  104.     if globalCount == 0:
  105.         if foundCombination and possibleCombinations > 1:
  106.             print "It is possible to get " + str(totalNugs) + " nuggets " + str(possibleCombinations) + " different ways"
  107.             print "Would you like to see all of the possible ways to get " + str(totalNugs) + " nuggets? (Y/N)"
  108.             while True:
  109.                 try:
  110.                     userFound1 = str(raw_input())
  111.                     if userFound1 == "Y" or userFound1 == "y":
  112.                         rerun = True
  113.                         globalCount = 1
  114.                         break
  115.                     elif userFound1 == "N" or userFound1 == "n":
  116.                         rerun = False
  117.                         break
  118.                     else:
  119.                         print "Please re-enter your decision in a acceptable format: (Y/N)"
  120.                 except ValueError:
  121.                     print "Please re-enter your decision in a acceptable format: (Y/N)\n"
  122.  
  123.         elif foundCombination and possibleCombinations == 1:
  124.             print "It is possible to get " + str(totalNugs) + " nuggets " + str(possibleCombinations) + " way"
  125.             print "Would you like to see all of the possible ways to get " + str(totalNugs) + " nuggets? (Y/N)"
  126.             while True:
  127.                 try:
  128.                     userFound1 = str(raw_input())
  129.                     if userFound1 == "Y" or userFound1 == "y":
  130.                         rerun = True
  131.                         globalCount = 1
  132.                         break
  133.                     elif userFound1 == "N" or userFound1 == "n":
  134.                         rerun = False
  135.                         break
  136.                     else:
  137.                         print "Please re-enter your decision in a acceptable format: (Y/N)"
  138.                 except ValueError:
  139.                     print "Please re-enter your decision in a acceptable format: (Y/N)\n"
  140.  
  141.         elif foundCombination == False and totalNugs != 1:
  142.             print "It is not possible to get " + str(totalNugs) + " nuggets by combining packs of 6, 9, and 20."
  143.             rerun = False
Advertisement
Add Comment
Please, Sign In to add comment