diff options
| -rw-r--r-- | coins2.py | 21 |
1 files changed, 8 insertions, 13 deletions
@@ -4,7 +4,7 @@ DIME = 0.10 NICKEL = 0.05 PENNY = 0.01 -def coins(total, coin): +def num_coins(total, coin): if total < coin: return 0 @@ -14,20 +14,15 @@ def coins(total, coin): def reduce(total, coin, number_of_coins): return round(total - (number_of_coins * coin), 2) -def calculate(total): - quarters = coins(total, QUARTER) - total = reduce(total, QUARTER, quarters) +def calculate(total, coins = [QUARTER, DIME, NICKEL, PENNY]): + result = [0, 0, 0, 0] - dimes = coins(total, DIME) - total = reduce(total, DIME, dimes) + for i, coin in enumerate(coins): + count = num_coins(total, coin) + total = reduce(total, coin, count) + result[i] = count - nickels = coins(total, NICKEL) - total = reduce(total, NICKEL, nickels) - - pennies = coins(total, PENNY) - total = reduce(total, PENNY, pennies) - - return [quarters, dimes, nickels, pennies] + return result def receipt(total): coins = calculate(total) |
