From c49f5d4a53532a4ebcdb41df2d32db243abefa1e Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 16 Apr 2025 18:04:01 -0600 Subject: refactor: extract a reduce function --- coins2.py | 12 ++++++++---- 1 file 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] -- cgit v1.2.3