From c050b761c94a079fcf43ebe46ed478b919033438 Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Sun, 1 Mar 2026 13:35:39 +0000 Subject: [PATCH 1/2] improve with caches --- .../fibonacci/fibonacci.py | 13 ++++++-- .../fibonacci/fibonacci_test.py | 1 - .../making_change/making_change.py | 31 +++++++++++++------ .../making_change/making_change_test.py | 1 - 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 60cc667..55175e6 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,4 +1,11 @@ -def fibonacci(n): - if n <= 1: + +def fibonacci(n, memo={}): + if n in memo: + return memo[n] + if n <= 1: return n - return fibonacci(n - 1) + fibonacci(n - 2) + memo[n] = fibonacci(n-1, memo) + fibonacci(n-2, memo) + return memo[n] + +# Time complexity: O(n) +# # Space complexity: O(n) due to the memo dictionary \ No newline at end of file diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci_test.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci_test.py index 8494e53..6df30c9 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci_test.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci_test.py @@ -1,5 +1,4 @@ import unittest - from fibonacci import fibonacci class FibonacciTest(unittest.TestCase): 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..5d60a66 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,26 @@ 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. - - 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 = {} + 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: +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. + Helper function with memoization. + Cache key is (total, index of first coin in list) — but since we pass + the coins list by slicing, we can cache using tuple(total, tuple(coins)). """ + + key = (total, tuple(coins)) + + if key in cache: + return cache[key] + if total == 0 or len(coins) == 0: return 0 @@ -26,7 +33,13 @@ 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 - return ways + + cache[key] = ways + return ways \ No newline at end of file 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..b94aa69 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 @@ -1,5 +1,4 @@ import unittest - from making_change import ways_to_make_change class MakingChangeTest(unittest.TestCase): From 0ea133aff73e03c4198d15b96d9211184ef6b855 Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Mon, 2 Mar 2026 15:26:01 +0000 Subject: [PATCH 2/2] making change update --- .../making_change/making_change.py | 63 ++++++++++--------- 1 file changed, 35 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 5d60a66..ee484a8 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -1,45 +1,52 @@ -from typing import List +from typing import Dict, Tuple + +COINS = [200, 100, 50, 20, 10, 5, 2, 1] + 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. + Returns the number of ways to make `total` using UK coin values. """ - cache = {} - return ways_to_make_change_helper(total, [200, 100, 50, 20, 10, 5, 2, 1], cache) + cache: Dict[Tuple[int, int], int] = {} + return _helper(total, 0, cache) -def ways_to_make_change_helper(total: int, coins: List[int], cache: dict) -> int: +def _helper(total: int, coin_index: int, cache: Dict[Tuple[int, int], int]) -> int: """ - Helper function with memoization. - Cache key is (total, index of first coin in list) — but since we pass - the coins list by slicing, we can cache using tuple(total, tuple(coins)). + Recursive helper using memoization. + + Parameters: + - total: remaining amount to form + - coin_index: index into COINS representing which coins we are allowed to use + - cache: memoization dictionary """ - key = (total, tuple(coins)) + # Base case: exact match + if total == 0: + return 1 + + # Base case: no coins left + if coin_index == len(COINS): + return 0 + key = (total, coin_index) + + # If we’ve already solved: return the cached result, We just return the stored answer instead of recomputing it if key in cache: return cache[key] - if total == 0 or len(coins) == 0: - return 0 - + # CORE LOGIC: 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:], - cache=cache - ) - ways += intermediate - count_of_coin += 1 + coin = COINS[coin_index] + + # Try using this coin 0, 1, 2, ... times + # The maximum number of this coin we could use without exceeding total. + max_count = total // coin + + # For each possible count of this coin, we compute the remaining amount and recursively call _helper for the next coin. + for count in range(max_count + 1): + remaining = total - (coin * count) + ways += _helper(remaining, coin_index + 1, cache) cache[key] = ways return ways \ No newline at end of file