From 77b822a6f5dcd3585b280282a847ddf9d8ff9702 Mon Sep 17 00:00:00 2001 From: JanefrancessC Date: Wed, 1 Jul 2026 00:17:16 +0100 Subject: [PATCH 1/3] feat: implement cache on fibonacci --- Sprint-2/improve_with_caches/fibonacci/fibonacci.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 60cc667..5871ddf 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,4 +1,12 @@ +_cache = {} + def fibonacci(n): + if n in _cache: + return _cache[n] + if n <= 1: return n - return fibonacci(n - 1) + fibonacci(n - 2) + + result = fibonacci(n - 1) + fibonacci(n - 2) + _cache[n] = result + return result From 2a038aa522ff4ae5af4ba141aac1904b396cd965 Mon Sep 17 00:00:00 2001 From: JanefrancessC Date: Wed, 1 Jul 2026 01:52:11 +0100 Subject: [PATCH 2/3] refactor: use memoisation to avoid recalulations --- .../fibonacci/fibonacci.py | 10 ++--- .../making_change/making_change.py | 39 ++++++++++--------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 5871ddf..d43c534 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,12 +1,10 @@ -_cache = {} - -def fibonacci(n): - if n in _cache: - return _cache[n] +def fibonacci(n, cache={}): + if n in cache: + return cache[n] if n <= 1: return n result = fibonacci(n - 1) + fibonacci(n - 2) - _cache[n] = result + cache[n] = result return result diff --git a/Sprint-2/improve_with_caches/making_change/making_change.py b/Sprint-2/improve_with_caches/making_change/making_change.py index 255612e..0d565b5 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -1,32 +1,35 @@ -from typing import List - - def ways_to_make_change(total: int) -> int: """ 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. 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. """ - return ways_to_make_change_helper(total, [200, 100, 50, 20, 10, 5, 2, 1]) + return ways_to_make_change_helper(total, 0) -def ways_to_make_change_helper(total: int, coins: List[int]) -> int: +def ways_to_make_change_helper(total: int, coin_index: int, cache={}) -> int: """ Helper function for ways_to_make_change to avoid exposing the coins parameter to callers. """ - if total == 0 or len(coins) == 0: + coins = (200, 100, 50, 20, 10, 5, 2, 1) + + key = (total, coin_index) + + if key in cache: + return cache[key] + + if total == 0: + return 1 + + if coin_index == len(coins): return 0 - ways = 0 - for coin_index in range(len(coins)): - coin = coins[coin_index] - count_of_coin = 1 - while coin * count_of_coin <= total: - total_from_coins = coin * count_of_coin - if total_from_coins == total: - ways += 1 - else: - intermediate = ways_to_make_change_helper(total - total_from_coins, coins=coins[coin_index+1:]) - ways += intermediate - count_of_coin += 1 + coin = coins[coin_index] + + if coin > total: + ways = ways_to_make_change_helper(total, coin_index + 1) + else: + ways = (ways_to_make_change_helper(total - coin, coin_index) + ways_to_make_change_helper(total, coin_index + 1)) + + cache[key] = ways return ways From 698cee172e8e836ef49ecde086af400707982c63 Mon Sep 17 00:00:00 2001 From: JanefrancessC Date: Wed, 1 Jul 2026 15:36:04 +0100 Subject: [PATCH 3/3] refactor: make COINS global variable --- .../improve_with_caches/making_change/making_change.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Sprint-2/improve_with_caches/making_change/making_change.py b/Sprint-2/improve_with_caches/making_change/making_change.py index 0d565b5..629aff6 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -1,3 +1,5 @@ +COINS = (200, 100, 50, 20, 10, 5, 2, 1) + def ways_to_make_change(total: int) -> int: """ 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. @@ -11,7 +13,6 @@ def ways_to_make_change_helper(total: int, coin_index: int, cache={}) -> int: """ Helper function for ways_to_make_change to avoid exposing the coins parameter to callers. """ - coins = (200, 100, 50, 20, 10, 5, 2, 1) key = (total, coin_index) @@ -21,15 +22,16 @@ def ways_to_make_change_helper(total: int, coin_index: int, cache={}) -> int: if total == 0: return 1 - if coin_index == len(coins): + if coin_index == len(COINS): return 0 - coin = coins[coin_index] + coin = COINS[coin_index] if coin > total: ways = ways_to_make_change_helper(total, coin_index + 1) + else: - ways = (ways_to_make_change_helper(total - coin, coin_index) + ways_to_make_change_helper(total, coin_index + 1)) + ways = (ways_to_make_change_helper(total - coin, coin_index)) + ways_to_make_change_helper(total, coin_index + 1) cache[key] = ways return ways