From cf6ba232ec38203a0a01be917acb9260c2fb2960 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 13 Oct 2026 19:30:04 +0100 Subject: [PATCH 1/4] implement closure-scoped memoization for fibonacci --- .../fibonacci/fibonacci.py | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 60cc6671..bdc66def 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,4 +1,21 @@ def fibonacci(n): - if n <= 1: - return n - return fibonacci(n - 1) + fibonacci(n - 2) + # Initialize the cache dictionary + cache = {} + + def helper(number): + # Check the cache at the start + if number in cache: + return cache[number] + + # Base case + if number <= 1: + return number + + # Compute the result (making sure number decreases!) + result = helper(number - 1) + helper(number - 2) + + # Store the result in the cache right before returning + cache[number] = result + return result + + return helper(n) \ No newline at end of file From 4d665b27afa3670116565041e244ddf9dc0a4af3 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 13 Oct 2026 23:20:17 +0100 Subject: [PATCH 2/4] Improve efficiency with cached results --- .../making_change/making_change.py | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 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 255612e5..f5336733 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -1,19 +1,28 @@ from typing import List - +# ===============> change function <============ 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]) - + # Initialize a cache dictionary + cache = {} + return ways_to_make_change_helper(total, [200, 100, 50, 20, 10, 5, 2, 1], cache) -def ways_to_make_change_helper(total: int, coins: List[int]) -> int: +# =================> change helper function <============== +def ways_to_make_change_helper(total: int, coins: List[int], cache: dict) -> int: """ Helper function for ways_to_make_change to avoid exposing the coins parameter to callers. """ + # Create unique key for this subproblem + cache_key = (total, tuple(coins)) + + # Return cached result if already calculated + if cache_key in cache: + return cache[cache_key] + if total == 0 or len(coins) == 0: return 0 @@ -26,7 +35,10 @@ def ways_to_make_change_helper(total: int, coins: List[int]) -> int: if total_from_coins == total: ways += 1 else: - intermediate = ways_to_make_change_helper(total - total_from_coins, coins=coins[coin_index+1:]) + intermediate = ways_to_make_change_helper(total - total_from_coins, coins=coins[coin_index+1:], cache=cache) ways += intermediate count_of_coin += 1 + + # Save result before returning + cache[cache_key] = ways return ways From 391df3545e779b8bd674d84055fa253b268c7b6a Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Thu, 15 Oct 2026 22:28:14 +0100 Subject: [PATCH 3/4] refactor(fibonacci): use function attribute for persistent memoization cache --- .../improve_with_caches/fibonacci/fibonacci.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index bdc66def..f63693f8 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,11 +1,12 @@ def fibonacci(n): - # Initialize the cache dictionary - cache = {} + # Initialize the cache as a function attribute if it doesn't exist yet + if not hasattr(fibonacci, "cache"): + fibonacci.cache = {} def helper(number): - # Check the cache at the start - if number in cache: - return cache[number] + # Check the function attribute cache at the start + if number in fibonacci.cache: + return fibonacci.cache[number] # Base case if number <= 1: @@ -14,8 +15,8 @@ def helper(number): # Compute the result (making sure number decreases!) result = helper(number - 1) + helper(number - 2) - # Store the result in the cache right before returning - cache[number] = result + # Store the result in the function attribute cache right before returning + fibonacci.cache[number] = result return result - return helper(n) \ No newline at end of file + return helper(n) From c7979d50a0ce2d9e70adbbaf95d8e5008d44612b Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Thu, 15 Oct 2026 22:57:31 +0100 Subject: [PATCH 4/4] refactor(ways_to_make_change): optimize cache keys with integer start_index --- .../making_change/making_change.py | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 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 f5336733..dae35ef0 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -1,5 +1,9 @@ from typing import List +# Defined once globally to avoid recreation overhead +COINS = [200, 100, 50, 20, 10, 5, 2, 1] + + # ===============> change function <============ def ways_to_make_change(total: int) -> int: """ @@ -9,36 +13,41 @@ def ways_to_make_change(total: int) -> int: """ # Initialize a cache dictionary cache = {} - return ways_to_make_change_helper(total, [200, 100, 50, 20, 10, 5, 2, 1], cache) + # Passing 0 (the starting index) instead of the full list + return ways_to_make_change_helper(total, 0, cache) + # =================> change helper function <============== -def ways_to_make_change_helper(total: int, coins: List[int], cache: dict) -> int: +def ways_to_make_change_helper(total: int, start_index: int, cache: dict) -> int: """ Helper function for ways_to_make_change to avoid exposing the coins parameter to callers. """ # Create unique key for this subproblem - cache_key = (total, tuple(coins)) + cache_key = (total, start_index) # Return cached result if already calculated if cache_key in cache: return cache[cache_key] - if total == 0 or len(coins) == 0: + if total == 0 or start_index >= len(COINS): return 0 ways = 0 - for coin_index in range(len(coins)): - coin = coins[coin_index] + # Iterate through the global COINS list using the integer index + for i in range(start_index, len(COINS)): + coin = COINS[i] 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:], cache=cache) + # Just pass an integer (i + 1) + intermediate = ways_to_make_change_helper( + total - total_from_coins, start_index=i + 1, cache=cache + ) ways += intermediate count_of_coin += 1 - # Save result before returning cache[cache_key] = ways return ways