London | 26-SDC-Mar | Ebrahim Beiati-Asl | Sprint 2 | improve_with_caches#156
Open
ebrahimbeiati wants to merge 1 commit into
Open
London | 26-SDC-Mar | Ebrahim Beiati-Asl | Sprint 2 | improve_with_caches#156ebrahimbeiati wants to merge 1 commit into
ebrahimbeiati wants to merge 1 commit into
Conversation
cjyuan
reviewed
May 17, 2026
Comment on lines
+1
to
+10
| def fibonacci(n, cache=None): | ||
| if cache is None: | ||
| cache = {} | ||
| if n in cache: | ||
| return cache[n] | ||
| if n <= 1: | ||
| return n | ||
| return fibonacci(n - 1) + fibonacci(n - 2) | ||
| else: | ||
| cache[n] = fibonacci(n - 1, cache) + fibonacci(n - 2, cache) | ||
| return cache[n] |
There was a problem hiding this comment.
Could also consider using inner function:
def fibonacci(n):
cache = {} # Accessible in fib_helper()
def fib_helper(n):
...
return fib_helper(n);
or just declare cache in the global scope since the values in the cache are still valid in subsequent call to fibonacci(n).
Comment on lines
+23
to
25
| if total < 0 or coin_index == len(coins): | ||
| return 0 | ||
|
|
There was a problem hiding this comment.
Note: When coin_index == len(coins) - 1, there is a quicker way to determine if the number of ways is 0 or 1 without involving cache lookup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Learners, PR Template
Self checklist