Advertisement
kl365

ps2ex1-4

Jan 28th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. # equation: 6a + 9b + 12c
  2.  
  3. # Equation is defined as the following:
  4. # x(a) + y(b) + z(c) = n
  5. # the goal is to determine what values of (a, b, c) are possible for given (x, y, z)
  6. def isComboPossible(n, x, y, z):
  7.     for a in range(0, (n / x) + 1 ):
  8.         for b in range (0, (n / y) + 1):
  9.             for c in range (0, (n / z) + 1):
  10.                 #print 'a: ', a ,' b: ', b ,' c: ', c
  11.                 if((x * a) + (y * b) + (z * c)) == n:
  12.                     return (a, b, c)
  13.                    
  14.     return False
  15.    
  16. # theorem: if it is possible to buy a, a+1...a+5 nuggets then it is possible to buy all
  17. # combinations > a
  18. #
  19. # problem, given that the theorem is true find the highest possible combination that is NOT
  20. # possible
  21. def findHighestImpossibleCombo(x, y, z):
  22.     currentHighest = 0       # stores failures as we come across them
  23.     possibleCount = 0        # keeps track of how many tests are positive in a row
  24.     comboNumberTest = 6;     # start at the lowest possible combo of nuggets which is 6
  25.     while possibleCount < 6:
  26.         if isComboPossible(comboNumberTest, x, y, z) == False:
  27.             currentHighest = comboNumberTest
  28.             possibleCount = 0
  29.         else:
  30.             possibleCount = possibleCount + 1
  31.        
  32.         comboNumberTest = comboNumberTest + 1
  33.    
  34.     return currentHighest
  35.            
  36.  
  37. #number = int(raw_input("How Many?\n"))
  38. packageSizes = (6, 9, 20)
  39.  
  40. #print 'PS2q1 TEST +++++++++++++++++++++++++++++++++\n\n\n'
  41. #chickenNuggetTest = isComboPossible(number, *packageSizes)
  42. #if chickenNuggetTest == False:
  43. #   print '\nnot possible'
  44. #else:
  45. #   print '\n6: ', chickenNuggetTest[0], '\n9: ', chickenNuggetTest[1], '\n20: ', chickenNuggetTest[2]
  46.  
  47. #print '\n\n PSq4 TEST  +++++++++++++++++++++++++++++++++++++\n\n\n'
  48.  
  49. print 'The Highest possible combination using ', packageSizes[0] ,', ', packageSizes[1] ,',', packageSizes[2] ,' is ', findHighestImpossibleCombo(*packageSizes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement