From 0569c1698c981a8bd58e445d66b825120cb9a81b Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Mon, 6 Jul 2026 00:55:53 +0100 Subject: [PATCH 1/4] Add memoisation to fibonacci --- Sprint-2/improve_with_caches/fibonacci/fibonacci.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 60cc667..7376d63 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,4 +1,10 @@ +cache = {0: 0, 1: 1} + + def fibonacci(n): - if n <= 1: - return n - return fibonacci(n - 1) + fibonacci(n - 2) + if n in cache: + return cache[n] + + cache[n] = fibonacci(n - 1) + fibonacci(n - 2) + + return cache[n] \ No newline at end of file From 6439c49f1f2a24fb3bfeb5f13b2f735944b9e69d Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Mon, 6 Jul 2026 00:56:58 +0100 Subject: [PATCH 2/4] Add memoisation to making change --- .../making_change/making_change.py | 35 +++++++++++++------ 1 file changed, 25 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 255612e..d12089c 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,47 @@ from typing import List +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. + 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. """ - return ways_to_make_change_helper(total, [200, 100, 50, 20, 10, 5, 2, 1]) + cache.clear() + 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: - """ - Helper function for ways_to_make_change to avoid exposing the coins parameter to callers. - """ + key = (total, tuple(coins)) + + if key in cache: + return cache[key] + if total == 0 or len(coins) == 0: 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 + ways += ways_to_make_change_helper( + total - total_from_coins, + coins[coin_index + 1:] + ) + count_of_coin += 1 - return ways + + cache[key] = ways + return ways \ No newline at end of file From 2a008a8848b89be1b1fd0f31ff160d9cfe88711a Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Tue, 14 Jul 2026 19:07:13 +0100 Subject: [PATCH 3/4] Remove global cache from fibonacci --- Sprint-2/improve_with_caches/fibonacci/fibonacci.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 7376d63..10faf71 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,10 +1,10 @@ -cache = {0: 0, 1: 1} +def fibonacci(n, cache=None): + if cache is None: + cache = {0: 0, 1: 1} - -def fibonacci(n): if n in cache: return cache[n] - cache[n] = fibonacci(n - 1) + fibonacci(n - 2) + cache[n] = fibonacci(n - 1, cache) + fibonacci(n - 2, cache) return cache[n] \ No newline at end of file From be178d2a123caad5efad50b07a0bf5f1f5fb80e5 Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Tue, 14 Jul 2026 19:13:20 +0100 Subject: [PATCH 4/4] Optimise making change memoisation --- .../making_change/making_change.py | 53 +++++++++---------- 1 file changed, 25 insertions(+), 28 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 d12089c..872bd10 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -1,6 +1,6 @@ from typing import List -cache = {} +COINS = [200, 100, 50, 20, 10, 5, 2, 1] def ways_to_make_change(total: int) -> int: @@ -8,40 +8,37 @@ 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.clear() - return ways_to_make_change_helper( - total, - [200, 100, 50, 20, 10, 5, 2, 1] - ) + cache = {} + def helper(total: int, start_index: int) -> int: + key = (total, start_index) -def ways_to_make_change_helper(total: int, coins: List[int]) -> int: - key = (total, tuple(coins)) + if key in cache: + return cache[key] - if key in cache: - return cache[key] + if total == 0 or start_index >= len(COINS): + return 0 - if total == 0 or len(coins) == 0: - return 0 + ways = 0 - ways = 0 + for coin_index in range(start_index, len(COINS)): + coin = COINS[coin_index] + count_of_coin = 1 - 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 - while coin * count_of_coin <= total: - total_from_coins = coin * count_of_coin + if total_from_coins == total: + ways += 1 + else: + ways += helper( + total - total_from_coins, + coin_index + 1 + ) - if total_from_coins == total: - ways += 1 - else: - ways += ways_to_make_change_helper( - total - total_from_coins, - coins[coin_index + 1:] - ) + count_of_coin += 1 - count_of_coin += 1 + cache[key] = ways + return ways - cache[key] = ways - return ways \ No newline at end of file + return helper(total, 0) \ No newline at end of file