summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-04-16 18:04:01 -0600
committermo khan <mo@mokhan.ca>2025-04-16 18:04:01 -0600
commitc49f5d4a53532a4ebcdb41df2d32db243abefa1e (patch)
tree6c3f50ee2b038501c26bb79390540ad958f8ffe4
parentd733b68808367799335bdacac732f91c168c0d85 (diff)
refactor: extract a reduce function
-rw-r--r--coins2.py12
1 files changed, 8 insertions, 4 deletions
diff --git a/coins2.py b/coins2.py
index bc23c56..39bf534 100644
--- a/coins2.py
+++ b/coins2.py
@@ -11,17 +11,21 @@ def coins(total, coin):
remainder = round(total % coin, 2)
return round((total - remainder) / coin)
+def reduce(total, coin, number_of_coins):
+ return round(total - (number_of_coins * coin), 2)
+
def calculate(total):
quarters = coins(total, QUARTER)
- total = round(total - (quarters * QUARTER), 2)
+ total = reduce(total, QUARTER, quarters)
dimes = coins(total, DIME)
- total = round(total - (dimes * DIME), 2)
+ total = reduce(total, DIME, dimes)
nickels = coins(total, NICKEL)
- total = round(total - (nickels * NICKEL), 2)
+ total = reduce(total, NICKEL, nickels)
- pennies = round(total * 100)
+ pennies = coins(total, PENNY)
+ total = reduce(total, PENNY, pennies)
print([quarters, dimes, nickels, pennies])
return [quarters, dimes, nickels, pennies]