Skip to content

Commit 1c6540a

Browse files
committed
Refactor ways_to_make_change for clarity and consistency in memoization
1 parent 93806b6 commit 1c6540a

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

Sprint-2/improve_with_caches/making_change/making_change.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ def ways_to_make_change(total: int) -> int:
66
77
For instance, there are two ways to make a value of 3: with 3x 1 coins, or with 1x 1 coin and 1x 2 coin.
88
"""
9+
coins = [200, 100, 50, 20, 10, 5, 2, 1]
910
memo: Dict[Tuple[int, int], int] = {}
10-
return ways_to_make_change_helper(total, [200, 100, 50, 20, 10, 5, 2, 1], memo)
11+
return ways_to_make_change_helper(total, coins, memo)
1112

1213

1314
def ways_to_make_change_helper(total: int, coins: List[int], memo: Dict[Tuple[int, int], int]) -> int:
@@ -25,18 +26,15 @@ def ways_to_make_change_helper(total: int, coins: List[int], memo: Dict[Tuple[in
2526
return memo[key]
2627

2728
ways = 0
28-
coin_index = 0
2929
for coin_index in range(len(coins)):
3030
coin = coins[coin_index]
3131
count_of_coin = 1
3232
while coin * count_of_coin <= total:
33-
total_from_coins = coin * count_of_coin
34-
remainder = total - total_from_coins
33+
remainder = total - coin * count_of_coin
3534
if remainder == 0:
3635
ways += 1
3736
else:
38-
intermediate = ways_to_make_change_helper(remainder, coins[coin_index + 1:], memo)
39-
ways += intermediate
37+
ways += ways_to_make_change_helper(remainder, coins[coin_index + 1:], memo)
4038
count_of_coin += 1
4139

4240
memo[key] = ways

0 commit comments

Comments
 (0)