-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added a fibonacci algorithm which uses memoization #5722
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
Conversation
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.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper
commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper review
to trigger the checks for only added pull request files@algorithms-keeper review-all
to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
cache = {} | ||
|
||
|
||
def fibo(n: int) -> int: |
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.
As there is no test file in this pull request nor any test function or class in the file maths/fibonacci_sequence_memoization.py
, please provide doctest for the function fibo
Please provide descriptive name for the parameter: n
@@ -0,0 +1,29 @@ | |||
cache: dict[int, int] = {} |
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.
Prefill the cache...
cache: dict[int, int] = {} | |
cache: dict[int, int] = {1: 1, 2: 1} |
elif num in [1, 2]: | ||
value = 1 | ||
else: | ||
value = fibo(num - 1) + fibo(num - 2) | ||
|
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.
elif num in [1, 2]: | |
value = 1 | |
else: | |
value = fibo(num - 1) + fibo(num - 2) | |
value = fibo(num - 1) + fibo(num - 2) |
@JDeepD btw, why is it chosen to define cache at the module level? Classical memoization technique enables creating an outer and inner function, something like:
... and then we do something like:
The code in this PR is perfectly workable, but what about showcasing the "classical" ways of solving the problems? |
Yeah sorry, I will change it right away. |
Rather than creating another object, wouldn't it be better if we just pass the
|
Wait I messed something while pushing |
Hmm, quite frankly, I never saw such an implementation. My checks led me to one classical book about competitive programming (link), where we can see such an implementation of memoization in C++:
This piece of code is structurally implemented as your PR: cache is defined at the module level. However, this example shows how to write the code in the fastest way (because writing inner and outer functions takes time in programming contests). However, if we look at some other example, we can see we create an outer and an inner function in all cases: because we re-use this code and it should be written well. Edit: regarding returning value, not function: it just then makes little sense to write an inner and outer function. The purpose of two of them is that each |
Yeah, I also think putting the |
OK, I'll take a look at it then :) |
Thnx. The new PR is here #5856 |
Describe your change:
Checklist:
Fixes: #{$ISSUE_NO}
.