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..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,32 +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. - - 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. + Returns the number of ways to make `total` using UK coin values. """ - return ways_to_make_change_helper(total, [200, 100, 50, 20, 10, 5, 2, 1]) + cache: Dict[Tuple[int, int], int] = {} + return _helper(total, 0, cache) -def ways_to_make_change_helper(total: int, coins: List[int]) -> int: +def _helper(total: int, coin_index: int, cache: Dict[Tuple[int, int], int]) -> int: """ - Helper function for ways_to_make_change to avoid exposing the coins parameter to callers. + 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 """ - if total == 0 or len(coins) == 0: + + # 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] + + # 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:]) - ways += intermediate - count_of_coin += 1 - return ways + 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 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):