diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 60cc6671..f53bd7cf 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,4 +1,12 @@ -def fibonacci(n): - if n <= 1: - return n - return fibonacci(n - 1) + fibonacci(n - 2) +class Fibonacci: + def __init__(self): + self.cache = {0: 0, 1: 1} + + def __call__(self, n): + if n in self.cache: + return self.cache[n] + + self.cache[n] = self(n - 1) + self(n - 2) + return self.cache[n] + +fibonacci = Fibonacci() diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci_test.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci_test.py index 8494e53c..a566e32d 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci_test.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci_test.py @@ -18,6 +18,8 @@ def test_3(self): def test_10(self): self.assertEqual(fibonacci(10), 55) + def test_50(self): + self.assertEqual(fibonacci(50), 12586269025) def test_200(self): self.assertEqual(fibonacci(200), 280571172992510140037611932413038677189525) diff --git a/Sprint-2/improve_with_caches/making_change/coin.py b/Sprint-2/improve_with_caches/making_change/coin.py new file mode 100644 index 00000000..e547cdbc --- /dev/null +++ b/Sprint-2/improve_with_caches/making_change/coin.py @@ -0,0 +1,5 @@ +class Coin: + def __init__(self, name: str, value: int): + self.name = name + self.value = value + 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 255612e5..fb68c134 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,41 @@ -from typing import List +from functools import lru_cache +from coin import Coin + +COINS = [ + Coin("200 coins", 200), + Coin("100 coins", 100), + Coin("50 coins", 50), + Coin("20 coins", 20), + Coin("10 coins", 10), + Coin("5 coins", 5), + Coin("2 coins", 2), + Coin("1 coin", 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. + Given access to coins with the values 1, 2, 5, 10, 20, 50, 100, 200, + returns the number of ways to make the total. """ - return ways_to_make_change_helper(total, [200, 100, 50, 20, 10, 5, 2, 1]) + @lru_cache(maxsize=None) + def helper(remaining: int, index: int) -> int: + + if remaining == 0: + return 1 + + if index == len(COINS): + return 0 + ways = 0 + coin = COINS[index] -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 - - 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 + max_count = remaining + + for count in range(max_count + 1): + new_remaining = remaining - count * coin.value + ways += helper(new_remaining, index + 1) + + return ways + + return helper(total, 0) \ 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 e4e0b74a..e04266ef 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 @@ -24,6 +24,8 @@ def test_7(self): def test_17(self): self.assertEqual(ways_to_make_change(17), 28) + def test_100(self): + self.assertEqual(ways_to_make_change(100), 4563) def test_9176(self): self.assertEqual(ways_to_make_change(9176), 628431158425225)