Skip to content

Commit afcae8c

Browse files
committed
feedback fix 2
1 parent d5efde9 commit afcae8c

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
cache = {}
2-
3-
def fibonacci(n):
1+
def fibonacci(n, cache = {}):
42
if n <= 1:
53
return n
64
if n in cache:
75
return cache[n]
86
else:
9-
cache[n] = fibonacci(n - 1) + fibonacci(n - 2)
7+
cache[n] = fibonacci(n - 1, cache) + fibonacci(n - 2, cache)
108
return cache[n]
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
11
from typing import List
2-
cache = {}
3-
COINS = [200, 100, 50, 20, 10, 5, 2, 1]
4-
COINT_TYPES_NUM = len(COINS)
52

63
def ways_to_make_change(total: int) -> int:
74
"""
85
Given access to coins with the values 1, 2, 5, 10, 20, 50, 100, 200, returns a count of all of the ways to make the passed total value.
96
107
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.
118
"""
12-
return ways_to_make_change_helper(total, 0)
9+
coins = [200, 100, 50, 20, 10, 5, 2, 1]
10+
return ways_to_make_change_helper(total, 0, coins)
1311

1412

15-
def ways_to_make_change_helper(total: int, coin_index: int) -> int:
13+
def ways_to_make_change_helper(total: int, coin_index: int, coins: List[int], cache = {}) -> int:
1614
"""
1715
Helper function for ways_to_make_change to avoid exposing the coins parameter to callers.
1816
"""
19-
if (total, coin_index) in cache:
20-
return cache[(total, coin_index)]
17+
coins_types_num = len(coins)
18+
index_total = (total, coin_index)
19+
if index_total in cache:
20+
return cache[index_total]
2121

2222
if total == 0:
2323
return 1
2424

25-
if coin_index == COINT_TYPES_NUM:
25+
if coin_index == coins_types_num:
2626
return 0
2727

2828

2929
ways = 0
30-
coin = COINS[coin_index]
30+
coin = coins[coin_index]
3131
count_of_coin = 0
3232
while count_of_coin * coin <= total:
3333
ways += ways_to_make_change_helper(
3434
total - count_of_coin * coin,
35-
coin_index + 1
35+
coin_index + 1,
36+
coins,
37+
cache
3638
)
3739
count_of_coin += 1
3840

39-
cache[(total, coin_index)] = ways
41+
cache[index_total] = ways
4042
return ways

0 commit comments

Comments
 (0)