From 020ef11854684ed37dc0cca6da7d8eaa484993da Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 21:34:19 +0000 Subject: [PATCH 01/21] create/define a dict that acts as my cache --- Sprint-2/improve_with_caches/fibonacci/fibonacci.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 60cc667..6f7892b 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,4 +1,6 @@ def fibonacci(n): + cache = {} + if n <= 1: return n return fibonacci(n - 1) + fibonacci(n - 2) From 9687e700c02c158c17a224765cd285ccd7f492e0 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 21:47:32 +0000 Subject: [PATCH 02/21] Update "Fibonacci" function to cache results when n <= 1 before returning the value --- Sprint-2/improve_with_caches/fibonacci/fibonacci.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 6f7892b..013426b 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -2,5 +2,7 @@ def fibonacci(n): cache = {} if n <= 1: + cache[n] = n return n + return fibonacci(n - 1) + fibonacci(n - 2) From 80bbd88a9f186e07ecd61862217de1494c33a8b8 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 21:49:47 +0000 Subject: [PATCH 03/21] Update "Fibonacci" function to cache results when n > 1 before returning its value (result) --- Sprint-2/improve_with_caches/fibonacci/fibonacci.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 013426b..fc38851 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -5,4 +5,6 @@ def fibonacci(n): cache[n] = n return n - return fibonacci(n - 1) + fibonacci(n - 2) + result = fibonacci(n - 1) + fibonacci(n - 2) + cache[n] = result + return result From 870d1b9da4b82f0548bcaa737f07123d40d48201 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 21:53:18 +0000 Subject: [PATCH 04/21] place cache outside the function otherwise it wont' store pairs as it should --- Sprint-2/improve_with_caches/fibonacci/fibonacci.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index fc38851..7013f91 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,5 +1,6 @@ + cache = {} + def fibonacci(n): - cache = {} if n <= 1: cache[n] = n From b5802e951e144c5cb71df7ecd38a8989dccc0a2a Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 21:59:50 +0000 Subject: [PATCH 05/21] return the cache in both conditions instead, so that it can be read from --- Sprint-2/improve_with_caches/fibonacci/fibonacci.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 7013f91..23410e4 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,11 +1,11 @@ - cache = {} +cache = {} def fibonacci(n): if n <= 1: cache[n] = n - return n + return cache result = fibonacci(n - 1) + fibonacci(n - 2) cache[n] = result - return result + return cache From 1429f7dcbdd27b78063440c6961d24e907a087ad Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 22:03:41 +0000 Subject: [PATCH 06/21] revert to the initial returns in both conditions to avoid bugs --- Sprint-2/improve_with_caches/fibonacci/fibonacci.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 23410e4..525089a 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -4,8 +4,8 @@ def fibonacci(n): if n <= 1: cache[n] = n - return cache + return n result = fibonacci(n - 1) + fibonacci(n - 2) cache[n] = result - return cache + return result From 2cf72518f2d7848ffe38b6a9695dc19c91e0202c Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 22:12:11 +0000 Subject: [PATCH 07/21] expand fibonacci function to handle when a key is already stored and thus return it's associated value --- Sprint-2/improve_with_caches/fibonacci/fibonacci.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 525089a..63016d7 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,6 +1,8 @@ cache = {} def fibonacci(n): + if n in cache: + return cache[n] if n <= 1: cache[n] = n From d85aec6fa42b4b548bbc64c91aa98ca1bbc20221 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Sat, 28 Feb 2026 00:34:54 +0000 Subject: [PATCH 08/21] Define cache dictionary for memoising results of total and coin inputs --- Sprint-2/improve_with_caches/making_change/making_change.py | 2 ++ 1 file changed, 2 insertions(+) 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..cf83503 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,7 @@ from typing import List +cache = {} + def ways_to_make_change(total: int) -> int: """ From 7477e9d6f792fd5166c6f8bec82df7db466d08eb Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Sat, 28 Feb 2026 00:39:05 +0000 Subject: [PATCH 09/21] Create a cache key from the current total and available coins --- Sprint-2/improve_with_caches/making_change/making_change.py | 6 +++--- 1 file changed, 3 insertions(+), 3 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 cf83503..a8b5aa1 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -3,6 +3,7 @@ cache = {} + 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. @@ -13,9 +14,8 @@ def ways_to_make_change(total: int) -> int: def ways_to_make_change_helper(total: int, coins: List[int]) -> int: - """ - Helper function for ways_to_make_change to avoid exposing the coins parameter to callers. - """ + key = (total, tuple(coins)) + if total == 0 or len(coins) == 0: return 0 From fa897c10cd46d96b547da6c1ee3f4d4ce1377485 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Sat, 28 Feb 2026 00:40:54 +0000 Subject: [PATCH 10/21] assigned the calcuated value (ways) to the cache key (key) --- Sprint-2/improve_with_caches/making_change/making_change.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 a8b5aa1..c25b825 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -15,7 +15,8 @@ def ways_to_make_change(total: int) -> int: def ways_to_make_change_helper(total: int, coins: List[int]) -> int: key = (total, tuple(coins)) - + cache[key] = ways + if total == 0 or len(coins) == 0: return 0 From 6f1f754b87980cfc8a2396e0e9661aa079413ae4 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Sat, 28 Feb 2026 01:14:36 +0000 Subject: [PATCH 11/21] Save the number of ways (the result) so that next time we see this same (total, coins) combination, we can reuse it. --- .../making_change/making_change.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 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 c25b825..2793536 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -2,20 +2,14 @@ cache = {} - - 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]) def ways_to_make_change_helper(total: int, coins: List[int]) -> int: key = (total, tuple(coins)) - cache[key] = ways + if total == 0 or len(coins) == 0: return 0 @@ -32,4 +26,7 @@ def ways_to_make_change_helper(total: int, coins: List[int]) -> int: intermediate = ways_to_make_change_helper(total - total_from_coins, coins=coins[coin_index+1:]) ways += intermediate count_of_coin += 1 + + cache[key] = ways + return ways From 0e824bb681510f63b2fb3924e66568f0a258270c Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Tue, 3 Mar 2026 22:18:43 +0000 Subject: [PATCH 12/21] Replace the code line that repeatedly creates the same sub-arrays. Use a hard-coded list with all the coins so that we can refer to any using their index --- Sprint-2/improve_with_caches/making_change/making_change.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 2793536..4a6e80b 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,7 @@ from typing import List +coins = [200, 100, 50, 20, 10, 5, 2, 1] + cache = {} def ways_to_make_change(total: int) -> int: @@ -16,7 +18,6 @@ def ways_to_make_change_helper(total: int, coins: List[int]) -> int: 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 From a771cf7ba8f8b6c6d0af607e3517a7a74c785a4b Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Tue, 3 Mar 2026 22:21:13 +0000 Subject: [PATCH 13/21] update hepler function argument from coin to coin_index to uniquely identify a subarray we're using --- Sprint-2/improve_with_caches/making_change/making_change.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 4a6e80b..aec5881 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -9,7 +9,7 @@ def ways_to_make_change(total: int) -> int: return ways_to_make_change_helper(total, [200, 100, 50, 20, 10, 5, 2, 1]) -def ways_to_make_change_helper(total: int, coins: List[int]) -> int: +def ways_to_make_change_helper(total: int, coin_index :int) -> int: key = (total, tuple(coins)) From 6ebb9f6bb3b0688a3c7ab54ea33ec63e0af83a66 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Tue, 3 Mar 2026 22:22:50 +0000 Subject: [PATCH 14/21] Update the return of function "ways_to_make_change" --- Sprint-2/improve_with_caches/making_change/making_change.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 aec5881..b5e14d6 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -6,7 +6,7 @@ def ways_to_make_change(total: int) -> int: - 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, coin_index :int) -> int: From ba0d360d17d5ee40d34e63f7081f18d5b01a1253 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Tue, 3 Mar 2026 22:25:09 +0000 Subject: [PATCH 15/21] Use coin index instead of coins tuple in cache key --- Sprint-2/improve_with_caches/making_change/making_change.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 b5e14d6..5ae93e4 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -10,7 +10,7 @@ def ways_to_make_change(total: int) -> int: def ways_to_make_change_helper(total: int, coin_index :int) -> int: - key = (total, tuple(coins)) + key = (total, coin_index) if total == 0 or len(coins) == 0: From 910f67eabb6a98f3b6363dbcb84d2b14b58b1f05 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Tue, 3 Mar 2026 22:29:02 +0000 Subject: [PATCH 16/21] update all reference to "coin" with "coins" the new hardcoded list --- Sprint-2/improve_with_caches/making_change/making_change.py | 6 +++--- 1 file changed, 3 insertions(+), 3 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 5ae93e4..81df9a6 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -13,14 +13,14 @@ def ways_to_make_change_helper(total: int, coin_index :int) -> int: key = (total, coin_index) - if total == 0 or len(coins) == 0: + if coin_index == len(coins): return 0 ways = 0 for coin_index in range(len(coins)): count_of_coin = 1 - while coin * count_of_coin <= total: - total_from_coins = coin * count_of_coin + while coins * count_of_coin <= total: + total_from_coins = coins * count_of_coin if total_from_coins == total: ways += 1 else: From 2d527ae041e658bd030f54e0e0cf17a72b20c1b9 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Tue, 3 Mar 2026 22:33:37 +0000 Subject: [PATCH 17/21] remove the for loop inside the helper function --- .../making_change/making_change.py | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 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 81df9a6..c362bf3 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -17,16 +17,17 @@ def ways_to_make_change_helper(total: int, coin_index :int) -> int: return 0 ways = 0 - for coin_index in range(len(coins)): - count_of_coin = 1 - while coins * count_of_coin <= total: - total_from_coins = coins * 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] + count_of_coin = 1 + while coins * count_of_coin <= total: + total_from_coins = coins * 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 cache[key] = ways From 08f9869936b00773fb8b4abb04a3d5b4544b645f Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Tue, 3 Mar 2026 22:40:15 +0000 Subject: [PATCH 18/21] updat the while lopp inside the helper function --- .../making_change/making_change.py | 14 ++++++-------- 1 file changed, 6 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 c362bf3..8757b4a 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -19,14 +19,12 @@ def ways_to_make_change_helper(total: int, coin_index :int) -> int: ways = 0 coin = coins[coin_index] - count_of_coin = 1 - while coins * count_of_coin <= total: - total_from_coins = coins * 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 = 0 + while count_of_coin * count_of_coin <= total: + ways += ways_to_make_change_helper( + total - count_of_coin * coin, + coin_index + 1 + ) count_of_coin += 1 cache[key] = ways From 716d81f44bca3b0cbec21ea731c4abc58cd83271 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Tue, 3 Mar 2026 22:42:30 +0000 Subject: [PATCH 19/21] Add cache lookup insde the helper function --- Sprint-2/improve_with_caches/making_change/making_change.py | 3 +++ 1 file changed, 3 insertions(+) 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 8757b4a..c7d1a23 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -11,6 +11,9 @@ def ways_to_make_change(total: int) -> int: def ways_to_make_change_helper(total: int, coin_index :int) -> int: key = (total, coin_index) + + if key in cache: + return cache[key] if coin_index == len(coins): From 994ee9f7a343bf4b5726fc317283c40d2a2b6ad6 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Tue, 3 Mar 2026 22:44:36 +0000 Subject: [PATCH 20/21] Add base case to return 1 when total is 0 --- Sprint-2/improve_with_caches/making_change/making_change.py | 3 +++ 1 file changed, 3 insertions(+) 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 c7d1a23..17d778f 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -15,6 +15,9 @@ def ways_to_make_change_helper(total: int, coin_index :int) -> int: if key in cache: return cache[key] + if total == 0: + return 1 + if coin_index == len(coins): return 0 From 4570e987392b46c841a6343f3e643b7ab4aad948 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Tue, 3 Mar 2026 22:46:42 +0000 Subject: [PATCH 21/21] Fix while loop condition in helper to correctly iterates --- Sprint-2/improve_with_caches/making_change/making_change.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 17d778f..084111a 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -26,7 +26,7 @@ def ways_to_make_change_helper(total: int, coin_index :int) -> int: coin = coins[coin_index] count_of_coin = 0 - while count_of_coin * count_of_coin <= total: + while count_of_coin * coin <= total: ways += ways_to_make_change_helper( total - count_of_coin * coin, coin_index + 1