Guest User

Untitled

a guest
Mar 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. def countCoinSlow(c):
  2. """
  3. THIS FUNCTION IS VERY SLOW AND INEFFICIENT, IT ONLY EXISTS TO SHOW THE IMPORTANCE OF EFFICIENT ALGORITHMS.
  4. Given a value in cents as a parameter, this function recursively calculates the number of combinations of
  5. pennies (1 cent), nickels (5 cents), dimes (10 cents),
  6. quarters (25 cents), and half dollar coins (50 cents)
  7. that add up to the given value.
  8. """
  9. combinations = []
  10. if c >= 1: # creates a branch where a penny is added to every combination
  11. subCombinations = countCoinSlow(c - 1)
  12. if len(subCombinations) != 0:
  13. for i in subCombinations:
  14. i.append(1)
  15. i.sort()
  16. else:
  17. subCombinations.append([1])
  18. adding = [x for x in subCombinations if x not in combinations] # removes combinations already in the list
  19. combinations.extend(adding)
  20. if c >= 5: # creates a branch where a nickel is added to every combination
  21. subCombinations = countCoinSlow(c - 5)
  22. if len(subCombinations) != 0:
  23. for i in subCombinations:
  24. i.append(5)
  25. i.sort()
  26. else:
  27. subCombinations.append([5])
  28. adding = [x for x in subCombinations if x not in combinations] # removes combinations already in the list
  29. combinations.extend(adding)
  30. if c >= 10: # creates a branch where a dime is added to every combination
  31. subCombinations = countCoinSlow(c - 10)
  32. if len(subCombinations) != 0:
  33. for i in subCombinations:
  34. i.append(10)
  35. i.sort()
  36. else:
  37. subCombinations.append([10])
  38. adding = [x for x in subCombinations if x not in combinations] # removes combinations already in the list
  39. combinations.extend(adding)
  40. if c >= 25: # creates a branch where a quarter is added to every combination
  41. subCombinations = countCoinSlow(c - 25)
  42. if len(subCombinations) != 0:
  43. for i in subCombinations:
  44. i.append(25)
  45. i.sort()
  46. else:
  47. subCombinations.append([25])
  48. adding = [x for x in subCombinations if x not in combinations] # removes combinations already in the list
  49. combinations.extend(adding)
  50. if c >= 50: # creates a branch where a half-dollar is added to every combination
  51. subCombinations = countCoinSlow(c - 50)
  52. if len(subCombinations) != 0:
  53. for i in subCombinations:
  54. i.append(50)
  55. i.sort()
  56. else:
  57. subCombinations.append([50])
  58. adding = [x for x in subCombinations if x not in combinations] # removes combinations already in the list
  59. combinations.extend(adding)
  60. return combinations
  61.  
  62.  
  63. def countCoinFast(c):
  64. """
  65. Given a value in cents as a parameter,
  66. this function uses dynamic programing to calculate the number of combinations of
  67. pennies (1 cent), nickels (5 cents), dimes (10 cents),
  68. quarters (25 cents), and half dollar coins (50 cents)
  69. that add up to the given value.
  70. """
  71. combinations = []
  72. amounts = [1, 5, 10, 25, 50]
  73. for i in range(0, c + 1):
  74. # This loop serves to initialize the 2-dimensional list
  75. combinations.append([])
  76. for amount in amounts:
  77. combinations[i].append(0)
  78. for amount in range(len(amounts)):
  79. for i in range(0, c + 1):
  80. if amount == 0: # Fill the penny row with 1's since any number can be represented only 1 way with pennies
  81. combinations[i][amount] = 1
  82. else:
  83. remains = i
  84. combinations[i][amount] = 0
  85. while remains >= 0:
  86. # Increment the currently calculated value by the number of combinations for the remaining cents
  87. # that was calculated using the previous type of coin
  88. combinations[i][amount] += combinations[remains][amount - 1]
  89. remains -= amounts[amount]
  90. return combinations[c][4] # The absolute last cell is the total number of unique coin combinations
  91.  
  92.  
  93. if __name__ == "__main__":
  94. cents = 0
  95. while cents != -1:
  96. try:
  97. cents = int(input("Please insert a cent amount: "))
  98. except ValueError:
  99. print("Not an Integer Value")
  100. continue
  101. method = input("Fast(f) or Slow(s)? ")
  102. if method == 'f':
  103. combinations = countCoinFast(cents)
  104. elif method == 's':
  105. combinations = len(countCoinSlow(cents))
  106. else:
  107. print("Invalid Method Type")
  108. continue
  109. print("Number of possible combinations = " + str(combinations))
Add Comment
Please, Sign In to add comment