diff --git a/Sprint-2/improve_with_caches/fibonacci/fibonacci.py b/Sprint-2/improve_with_caches/fibonacci/fibonacci.py index 60cc6671..5a351593 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) +def fibonacci(n,memo = {}): + if n in memo: + return memo[n] + if n <= 2: + return 1 + memo[n]=fibonacci(n - 1,memo) + fibonacci(n - 2,memo) + return memo[n] + + +print(fibonacci(8)) +print(fibonacci(50)) +print(fibonacci(100)) 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..0a8fe3e9 100644 --- a/Sprint-2/improve_with_caches/making_change/making_change.py +++ b/Sprint-2/improve_with_caches/making_change/making_change.py @@ -14,9 +14,20 @@ 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 + """ + + dp = [0] * (total+1) + dp[0] =1 + for coin in coins: + for amount in range(coin,total+1): + dp[amount] += dp[amount-coin] + return dp[total] + + """ ways = 0 for coin_index in range(len(coins)): coin = coins[coin_index] @@ -30,3 +41,8 @@ def ways_to_make_change_helper(total: int, coins: List[int]) -> int: ways += intermediate count_of_coin += 1 return ways + """ + +print(ways_to_make_change(7)) +print(ways_to_make_change(17)) +print(ways_to_make_change(9176))