diff options
| author | mo khan <mo@mokhan.ca> | 2025-04-16 18:04:01 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-04-16 18:04:01 -0600 |
| commit | c49f5d4a53532a4ebcdb41df2d32db243abefa1e (patch) | |
| tree | 6c3f50ee2b038501c26bb79390540ad958f8ffe4 | |
| parent | d733b68808367799335bdacac732f91c168c0d85 (diff) | |
refactor: extract a reduce function
| -rw-r--r-- | coins2.py | 12 |
1 files changed, 8 insertions, 4 deletions
@@ -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] |
