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
16 changes: 12 additions & 4 deletions Sprint-2/improve_with_caches/fibonacci/fibonacci.py
Original file line number Diff line number Diff line change
@@ -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]
Comment on lines +1 to +7

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The spacing around parameters/arguments, and around operators are not consistent. Could you use a formatter to keep the format consistent?



print(fibonacci(8))
print(fibonacci(50))
print(fibonacci(100))
16 changes: 16 additions & 0 deletions Sprint-2/improve_with_caches/making_change/making_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Comment on lines +20 to +29

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The code on lines 22-28 implements a totally different approach. While this approach is more efficient, implementing it beats the purpose of the exercise -- identify and caching reusable computed values in an implementation and reuse them.

Could you improve the original code instead?

"""
ways = 0
for coin_index in range(len(coins)):
coin = coins[coin_index]
Expand All @@ -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))
Loading