Skip to content

Commit 2f5217e

Browse files
committed
Implementation o fibonnaci sequence
1 parent 134e683 commit 2f5217e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1+
cache = {}
2+
13
def fibonacci(n):
4+
if n in cache:
5+
return cache[n]
6+
27
if n <= 1:
3-
return n
4-
return fibonacci(n - 1) + fibonacci(n - 2)
8+
result = n
9+
else:
10+
result = fibonacci(n - 1) + fibonacci(n - 2)
11+
12+
cache[n] = result
13+
return result

Sprint-2/improve_with_caches/making_change/making_change.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from typing import List
22

33

4+
cache = {}
5+
46
def ways_to_make_change(total: int) -> int:
57
"""
68
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.
@@ -14,6 +16,11 @@ def ways_to_make_change_helper(total: int, coins: List[int]) -> int:
1416
"""
1517
Helper function for ways_to_make_change to avoid exposing the coins parameter to callers.
1618
"""
19+
# Create a cache key from total and coins tuple
20+
cache_key = (total, tuple(coins))
21+
if cache_key in cache:
22+
return cache[cache_key]
23+
1724
if total == 0 or len(coins) == 0:
1825
return 0
1926

@@ -29,4 +36,6 @@ def ways_to_make_change_helper(total: int, coins: List[int]) -> int:
2936
intermediate = ways_to_make_change_helper(total - total_from_coins, coins=coins[coin_index+1:])
3037
ways += intermediate
3138
count_of_coin += 1
39+
40+
cache[cache_key] = ways
3241
return ways

0 commit comments

Comments
 (0)