From 4fb5c721cd0605a777e78541ea3f9c6b2ed2f727 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Mon, 13 Oct 2025 17:12:36 +0100 Subject: [PATCH 1/5] improved fibonacci --- Sprint-2/improve_with_caches/fibonacci/fibonacci.py | 9 ++++++++- 1 file changed, 8 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..746c38f 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,4 +1,11 @@ +cache = {} + + def fibonacci(n): if n <= 1: return n - return fibonacci(n - 1) + fibonacci(n - 2) + if n not in cache: + cache[n] = fibonacci(n - 1) + fibonacci(n - 2) + return cache[n] + else: + return cache[n] From d66ee9b5cf9080fdf7a52da3e199f47f157583c3 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Mon, 13 Oct 2025 17:16:16 +0100 Subject: [PATCH 2/5] improved making change with cache --- Sprint-2/improve_with_caches/making_change/making_change.py | 4 +++- 1 file changed, 3 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 255612e..a941cb1 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,9 @@ 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 :] + ) ways += intermediate count_of_coin += 1 return ways From 9561f642e0e8f4a7da48e10328c8b1e2685e866e Mon Sep 17 00:00:00 2001 From: Droid-An Date: Mon, 13 Oct 2025 17:17:59 +0100 Subject: [PATCH 3/5] actually done making change improvement --- .../making_change/making_change.py | 14 +++++++++++--- 1 file changed, 11 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 a941cb1..1a36c8b 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -10,6 +10,9 @@ def ways_to_make_change(total: int) -> int: 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: """ Helper function for ways_to_make_change to avoid exposing the coins parameter to callers. @@ -26,9 +29,14 @@ 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 :] - ) + key = (total - total_from_coins, tuple(coins[coin_index + 1 :])) + if key not in cache: + cache[key] = ways_to_make_change_helper( + total - total_from_coins, coins=coins[coin_index + 1 :] + ) + intermediate = cache[key] + else: + intermediate = cache[key] ways += intermediate count_of_coin += 1 return ways From bcc9ee48d6e48b6359c3edaa012d2979b65ffbf7 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Mon, 26 Jan 2026 21:25:18 +0000 Subject: [PATCH 4/5] refactor fibonacci function to simplify return statements --- Sprint-2/improve_with_caches/fibonacci/fibonacci.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 746c38f..e4063b7 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -6,6 +6,4 @@ def fibonacci(n): return n if n not in cache: cache[n] = fibonacci(n - 1) + fibonacci(n - 2) - return cache[n] - else: - return cache[n] + return cache[n] From c98d800ed3f6247f39d22436f05336f850da8b98 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Wed, 28 Jan 2026 16:26:13 +0000 Subject: [PATCH 5/5] refactor ways_to_make_change function to improve caching --- .../making_change/making_change.py | 53 +++++++------------ .../making_change/making_change_test.py | 2 + 2 files changed, 20 insertions(+), 35 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 1a36c8b..bd5fe96 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -1,42 +1,25 @@ -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. + cache = {} + coins = (200, 100, 50, 20, 10, 5, 2, 1) - 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 helper(remaining: int, start: int) -> int: + if remaining == 0: + return 1 + if remaining < 0 or start == len(coins): + return 0 + key = (remaining, start) + if key in cache: + return cache[key] -cache = {} + coin = coins[start] + ways = 0 + max_count = remaining // coin + for count in range(max_count + 1): + ways += helper(remaining - count * coin, start + 1) -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. - """ - if total == 0 or len(coins) == 0: - return 0 + cache[key] = ways + return ways - 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: - key = (total - total_from_coins, tuple(coins[coin_index + 1 :])) - if key not in cache: - cache[key] = ways_to_make_change_helper( - total - total_from_coins, coins=coins[coin_index + 1 :] - ) - intermediate = cache[key] - else: - intermediate = cache[key] - ways += intermediate - count_of_coin += 1 - return ways + return helper(total, 0) diff --git a/Sprint-2/improve_with_caches/making_change/making_change_test.py b/Sprint-2/improve_with_caches/making_change/making_change_test.py index e4e0b74..70275dc 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change_test.py +++ b/Sprint-2/improve_with_caches/making_change/making_change_test.py @@ -2,6 +2,7 @@ from making_change import ways_to_make_change + class MakingChangeTest(unittest.TestCase): def test_1(self): # 1x 1p @@ -27,5 +28,6 @@ def test_17(self): def test_9176(self): self.assertEqual(ways_to_make_change(9176), 628431158425225) + if __name__ == "__main__": unittest.main()