Skip to content

Commit 5471bb6

Browse files
committed
improve with caches
1 parent 99db946 commit 5471bb6

File tree

4 files changed

+61
-17
lines changed

4 files changed

+61
-17
lines changed
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
def fibonacci(n):
1+
2+
3+
def fibonacci(n, cache={}): # Add a cache parameter to store previously computed Fibonacci values
4+
if n in cache: # Check if the Fibonacci value for n is already in the cache
5+
return cache[n]
6+
27
if n <= 1:
38
return n
4-
return fibonacci(n - 1) + fibonacci(n - 2)
9+
10+
result = fibonacci(n - 1, cache) + fibonacci(n - 2, cache) # Recursive calls to compute Fibonacci values for n-1 and n-2, passing the cache to store results
11+
cache[n] = result # Store the computed Fibonacci value for n in the cache before returning it
12+
return result

Sprint-2/improve_with_caches/fibonacci/fibonacci_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import unittest
2-
32
from fibonacci import fibonacci
43

54
class FibonacciTest(unittest.TestCase):
Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,70 @@
1-
from typing import List
1+
from typing import List, Tuple, Dict
2+
from typing import List, Tuple, Dict
23

34

4-
def ways_to_make_change(total: int) -> int:
5+
def ways_to_make_change(total: int) -> int:
56
"""
67
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.
78
89
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.
910
"""
10-
return ways_to_make_change_helper(total, [200, 100, 50, 20, 10, 5, 2, 1])
11+
return ways_to_make_change_helper( # Call the helper function with the total, the list of coin denominations, and an empty cache
12+
total,
13+
[200, 100, 50, 20, 10, 5, 2, 1],
14+
cache={}
15+
)
1116

17+
from typing import List, Tuple, Dict
1218

13-
def ways_to_make_change_helper(total: int, coins: List[int]) -> int:
14-
"""
15-
Helper function for ways_to_make_change to avoid exposing the coins parameter to callers.
16-
"""
17-
if total == 0 or len(coins) == 0:
19+
20+
def ways_to_make_change(total: int) -> int:
21+
return ways_to_make_change_helper(
22+
total,
23+
[200, 100, 50, 20, 10, 5, 2, 1],
24+
cache={}
25+
)
26+
27+
28+
def ways_to_make_change_helper(
29+
total: int,
30+
coins: List[int],
31+
cache: Dict[Tuple[int, Tuple[int, ...]], int]
32+
) -> int:
33+
34+
# Convert coins list to tuple so it can be used as dict key
35+
key = (total, tuple(coins))
36+
37+
if key in cache:
38+
return cache[key]
39+
40+
if total == 0:
41+
return 1
42+
43+
if len(coins) == 0:
1844
return 0
1945

2046
ways = 0
47+
48+
# Iterate through each coin denomination and count the number of ways to make change using that denomination
49+
# and the remaining coins
2150
for coin_index in range(len(coins)):
2251
coin = coins[coin_index]
2352
count_of_coin = 1
24-
while coin * count_of_coin <= total:
53+
54+
while coin * count_of_coin <= total: # While the total value of the current coin denomination multiplied by the count of that coin is less than or equal to the total, we can continue to count ways to make change using that denomination
2555
total_from_coins = coin * count_of_coin
26-
if total_from_coins == total:
56+
57+
if total_from_coins == total: #
2758
ways += 1
2859
else:
29-
intermediate = ways_to_make_change_helper(total - total_from_coins, coins=coins[coin_index+1:])
30-
ways += intermediate
31-
count_of_coin += 1
60+
ways += ways_to_make_change_helper( #
61+
total - total_from_coins,
62+
coins[coin_index + 1:],
63+
cache
64+
)
65+
66+
count_of_coin += 1 # Increment the count of the current coin denomination to check for the next possible combination of coins
67+
68+
cache[key] = ways
3269
return ways
70+

Sprint-2/improve_with_caches/making_change/making_change_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import unittest
2-
32
from making_change import ways_to_make_change
43

54
class MakingChangeTest(unittest.TestCase):

0 commit comments

Comments
 (0)