Repository navigation
London | 26-SDC-Jul | AYK | Sprint 2 | Improve code with caches #270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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] | ||
|
|
||
|
|
||
| print(fibonacci(8)) | ||
| print(fibonacci(50)) | ||
| print(fibonacci(100)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
|
@@ -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)) | ||
There was a problem hiding this comment.
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?