diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 60cc6671..90c196f6 100644 --- a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py +++ b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py @@ -1,4 +1,9 @@ def fibonacci(n): if n <= 1: return n - return fibonacci(n - 1) + fibonacci(n - 2) + nums = [0, 1] + i = 0 + while i < n: + nums.append(nums[len(nums) - 1] + nums[len(nums) - 2]) + i += 1 + return nums[n] 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..c77af0f1 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -13,20 +13,12 @@ def ways_to_make_change(total: int) -> int: 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 + """ + ways = [0] * (total + 1) + ways[0] = 1 + + for coin in coins: + for amount in range(coin, total + 1): + ways[amount] += ways[amount - coin] + + return ways[total]