Advertisement
Guest User

Problem Set 2

a guest
May 6th, 2011
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.39 KB | None | 0 0
  1. #Function Purpose: To test if the number of nuggets is evenly divisible by any
  2. #                  of the packs sold
  3.  
  4. #Function Operations:  iterates through the tuple containing the nugget packs,
  5. #                      each time checking to see if the number of nuggets is
  6. #                      divisible by the pack, if it is, then it returns True,
  7. #                      otherwise, it returns False
  8. def divisibilityTester(factorsTuple, integer_test):
  9.   for n in factorsTuple:
  10.     if integer_test%n == 0:
  11.       return True
  12.   return False
  13.  
  14. #Function Purpose:  To generate a list of all possible results of the purchases
  15. #                   of nuggets  (like how 37 nuggets can be subtracted down,
  16. #                   using a pack of 9, to 28, then to 8 using a pack of 20)
  17. #                   Similar to branching:  (example uses packs of 6, 9, and 20)
  18. #                                                28
  19. #                                       /          |        \
  20. #                                      8           19       22
  21. #                                     /  |  \   /  |  \   /  |  \
  22. #                                   -12  -1  2  -1 10 13  2  13 16
  23. #NC = Not counted (due to negativity)
  24. #                                    NC  NC     NC
  25. #Tree not completed yet, its just an example of the first steps taken
  26.  
  27. #Function Operations:  Takes the starting list (passed to it by another function
  28. #                      , in this case "chickenTester") and iterates through it.
  29. #                      During each iteration the function iterates through the
  30. #                      packs tuple, each time appending (if the result is
  31. #                      non-negative) the difference between the element,
  32. #                      and each individual pack from the tuple.  Finally, it
  33. #                      returns the list of possible non-negative results from
  34. #                      purchases of nugget packs.
  35. def listMaker(quantityTuple, startingList):
  36.   for element in startingList:
  37.     if element > 0:
  38.       for quantity in quantityTuple:
  39.         if element - quantity > 0:
  40.           startingList.append(element - quantity)
  41.   return startingList
  42. #Function Purpose:  To use the output of the other two functions to judge whether
  43. #                   or not it is possible to purchase any given number of
  44. #                   in an exact quantity
  45.  
  46. #Function Operations:  Takes the packs tuple and the number of nuggets in
  47. #                      question as input.  Tests whether or not the nuggets in
  48. #                      question are able to be bought using just one type of
  49. #                      pack using the divisibilityTester function.  Returning
  50. #                      True if it is.  If the purchase isn't so simple, it checks
  51. #                      each element of the possibilities list to see if it is
  52. #                      divisible by any of the packs (using divisibilityTester).
  53. #                      If no evenly divisible solutions exist, then it returns
  54. #                      false
  55. def chickenTester(quantityTuple, testQuantity):
  56.   if divisibilityTester(quantityTuple, testQuantity) == True:
  57.     return True
  58.   else:
  59.     for possibility in listMaker(quantityTuple, [testQuantity]):
  60.       if divisibilityTester(quantityTuple, possibility) == True:
  61.         return True
  62.   return False
  63.  
  64.  
  65. #Function Purpose:  To collect and provide sufficient information to the other
  66. #                   functions, and to use the other functions to solve the prompt
  67.  
  68.  
  69. #Function Operations:  Sets the biggestSoFar counter (as suggested by the template)
  70. #                      and then uses the range (as provided by the template) to
  71. #                      perform a check on every quantity to find the largest
  72. #                      non-purchasable number of nuggets.
  73. def main():
  74.   pack1 = int(raw_input("What is the 1st available pack?"))
  75.   pack2 = int(raw_input("What is the 2nd available pack?"))
  76.   pack3 = int(raw_input("What is the 3rd available pack?"))
  77.  
  78.   biggestSoFar = 0
  79.   for n in range(1,150):
  80.     if chickenTester((pack1,pack2,pack3), n) == False:
  81.       biggestSoFar = n
  82.   print "Given package sizes", pack1, ",", pack2, ", and", pack3, ", the largest number of McNuggets that cannot be bought in exact quantity is: ", biggestSoFar
  83.  
  84.  
  85. #Boilerplate code to initiate main() on startup
  86. if __name__ == '__main__':
  87.   main()
  88. #Largest non-purchasable number of nuggets that this program finds is 43
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement