Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Sprint-2/improve_with_caches/fibonacci/fibonacci.py
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +4 to +8

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This iterative approach would beat the purpose of the exercise -- to use cache to improve the original recursive function.

Could you update your code to use a cache to improve the original implementation?

return nums[n]
26 changes: 9 additions & 17 deletions Sprint-2/improve_with_caches/making_change/making_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Comment on lines +17 to +22

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also replace this iterative version?


return ways[total]
Loading