Guest User

Untitled

a guest
May 26th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. import cs50
  2.  
  3. # Pseudo
  4. # - divide total by a coin amount
  5. # - keep dividing it by increments of that coin, count each time
  6. # - subtract from total each time, amount left undivided
  7. # - move the amount left from coin to coin
  8.  
  9.  
  10. def greedy():
  11. # set to two arbitraily to run
  12. # total to divide in change
  13. amount = cs50.get_float("Enter amount:")
  14. amount = amount * 100
  15. print(f"amount {amount}")
  16. # amount of coins
  17. change = 0
  18. # coin
  19. quarter = 25
  20. # amount after each interation left
  21. left = amount
  22. # total being accumulated against amount
  23. totalDivider = quarter
  24. divQuarterCheck = 0
  25. # get 25s
  26. # if amount is disible by 25, set checker
  27. if(amount == quarter):
  28. change += 1
  29. print(f"change: {change}")
  30. return change
  31. elif(amount / quarter > 1):
  32. divQuarterCheck = amount /quarter
  33. else:
  34. print("not divisible by 25")
  35. divQuarterCheck = amount /quarter
  36. while (divQuarterCheck >= 1):
  37. # each time divided check more than one
  38. # add double adder
  39. divQuarterCheck = (amount - totalDivider) / quarter
  40. # add another value to itself
  41. # if still divisible, go on, if not stop
  42. totalDivider += quarter
  43. # print(f"totalDivide: {totalDivider}")
  44. # print (divQuarterCheck)
  45. change += 1
  46. left -= quarter
  47. # print(left)
  48. print(f"{left} after quarters")
  49. dime = 10
  50. totalDivider = dime
  51. divDimeCheck = 0
  52. if(left == dime):
  53. change += 1
  54. print(f"change: {change}")
  55. return change
  56. elif(left / dime > 1):
  57. divDimeCheck = left /dime
  58. else:
  59. print("not divisible by 10")
  60. while (divDimeCheck >= 1):
  61. # each time divided check more than one
  62. # add double adder
  63. divDimeCheck = (left - totalDivider) / dime
  64. # add another value to itself
  65. # if still divisible, go on, if not stop
  66. totalDivider += dime
  67. # print(f"totalDivide: {totalDivider}")
  68. # print (divDimeCheck)
  69. change += 1
  70.  
  71. left -= dime
  72. # print(f"left:{left}")
  73. # divDimeCheck = 0
  74. print(f"{left} after dimes")
  75.  
  76. nickel = 5
  77. # if(left == nickel):
  78. # print("true")
  79. totalDivider = nickel
  80. divNickelCheck = 0
  81. if(left == nickel):
  82. change += 1
  83. print("true")
  84. print(f"change: {change}")
  85. return change
  86. elif(left / nickel > 1):
  87. divNickelCheck = left / nickel
  88. print("true")
  89. else:
  90. print("not divisible by 5")
  91. while (divNickelCheck >= 1):
  92. # each time divided check more than one
  93. # add double adder
  94. divNickelCheck = (left - totalDivider) / nickel
  95. # add another value to itself
  96. # if still divisible, go on, if not stop
  97. totalDivider += nickel
  98. # print(f"totalDivide: {totalDivider}")
  99. # print (divNickelCheck)
  100. change += 1
  101. left -= nickel
  102. # print(f"left:{left}")
  103. print(f"{left} after nickels")
  104.  
  105.  
  106. penny = 1
  107. divPennyCheck = 0
  108. totalDivider = penny
  109. if(left == penny):
  110. change += 1
  111. print(f"change: {change}")
  112. return change
  113. elif(left / penny > penny):
  114. divPennyCheck = left / penny
  115. else:
  116. print('not divisible by 1')
  117.  
  118. while (left):
  119. # each time divided check more than one
  120. # add double adder
  121. divPennyCheck = (left - totalDivider) / penny
  122. # add another value to itself
  123. # if still divisible, go on, if not stop
  124. totalDivider += penny
  125. # print(f"totalDivide: {totalDivider}")
  126. print (divPennyCheck)
  127. change += 1
  128. left -= penny
  129. print(f"{left} after > penny")
  130.  
  131. # if(left == penny):
  132. # left = left - penny
  133. # change += 1
  134. # print(f"change:{change}")
  135. # print(f"{left} after == penny")
  136.  
  137.  
  138.  
  139. print(f"change: {change}")
  140. return change
  141.  
  142. greedy()
  143.  
  144. # def get_input():
  145. # amount = cs50.get_float("Enter amount:")
  146. # amount = amount * 100
  147. # print(f"amount {amount}")
  148.  
  149. # get_input()
Add Comment
Please, Sign In to add comment