From 020ef11854684ed37dc0cca6da7d8eaa484993da Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Fri, 27 Feb 2026 21:34:19 +0000 Subject: [PATCH 01/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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