Skip to content

Commit f8eea94

Browse files
committed
Refactor ways_to_make_change to use a fixed COINS tuple and simplify parameters
1 parent 717948e commit f8eea94

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

‎Sprint-2/improve_with_caches/making_change/making_change.py‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
1-
from typing import List
2-
1+
COINS = (200, 100, 50, 20, 10, 5, 2, 1)
32
cache = {}
43

54
def ways_to_make_change(total: int) -> int:
6-
coins = [200, 100, 50, 20, 10, 5, 2, 1]
7-
return ways_to_make_change_helper(total, coins)
5+
return ways_to_make_change_helper(total, 0)
86

97

10-
def ways_to_make_change_helper(total: int, coins: List[int]) -> int:
8+
def ways_to_make_change_helper(total: int, coin_index: int) -> int:
119
if total == 0:
1210
return 1
1311

14-
if total < 0 or len(coins) == 0:
12+
if total < 0 or coin_index == len(COINS):
1513
return 0
1614

17-
key = (total, tuple(coins))
15+
key = (total, coin_index)
1816

1917
if key in cache:
2018
return cache[key]
2119

22-
coin = coins[0]
20+
coin = COINS[coin_index]
2321

2422
ways = 0
2523

2624
for count in range(total // coin + 1):
2725
remaining = total - count * coin
2826

29-
ways += ways_to_make_change_helper(remaining, coins[1:])
27+
ways += ways_to_make_change_helper(remaining, coin_index + 1)
3028

3129
cache[key] = ways
3230

0 commit comments

Comments
 (0)