Skip to content

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

Closed
wants to merge 0 commits into from
Closed

Added a fibonacci algorithm which uses memoization #5722

wants to merge 0 commits into from

Conversation

JDeepD
Copy link
Contributor

@JDeepD JDeepD commented Oct 31, 2021

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@JDeepD JDeepD requested a review from Kush1101 as a code owner October 31, 2021 14:52
@ghost ghost added awaiting reviews This PR is ready to be reviewed require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required labels Oct 31, 2021
Copy link

@ghost ghost left a 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:
Copy link

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

@ghost ghost added tests are failing Do not merge until tests pass and removed require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required tests are failing Do not merge until tests pass labels Oct 31, 2021
@@ -0,0 +1,29 @@
cache: dict[int, int] = {}
Copy link
Member

Choose a reason for hiding this comment

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

Prefill the cache...

Suggested change
cache: dict[int, int] = {}
cache: dict[int, int] = {1: 1, 2: 1}

Comment on lines 19 to 23
elif num in [1, 2]:
value = 1
else:
value = fibo(num - 1) + fibo(num - 2)

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
elif num in [1, 2]:
value = 1
else:
value = fibo(num - 1) + fibo(num - 2)
value = fibo(num - 1) + fibo(num - 2)

@ghost ghost added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Nov 4, 2021
@andrei-polukhin
Copy link

@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:

def fibo() -> Callable:
    cache: dict[int, int] = {1: 1, 2: 1}

    def func_(n: int) -> int:
        nonlocal cache
        ...

    return func_

... and then we do something like:

fibonacci = fibo()
fibonacci(5)

The code in this PR is perfectly workable, but what about showcasing the "classical" ways of solving the problems?

@JDeepD
Copy link
Contributor Author

JDeepD commented Nov 28, 2021

@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:

def fibo() -> Callable:
    cache: dict[int, int] = {1: 1, 2: 1}

    def func_(n: int) -> int:
        nonlocal cache
        ...

    return func_

... and then we do something like:

fibonacci = fibo()
fibonacci(5)

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.

@ghost ghost added awaiting reviews This PR is ready to be reviewed and removed awaiting changes A maintainer has requested changes to this PR labels Nov 28, 2021
@JDeepD
Copy link
Contributor Author

JDeepD commented Nov 28, 2021

fibonacci = fibo()
fibonacci(5)

Rather than creating another object, wouldn't it be better if we just pass the num from fibonacci to fibo directly?
Something like:

def fibo(num: int) -> int: 
       cache: dict[int, int] = {}
       def fibo(num:int) -> int:
           ...
       return fibo(num) 


@JDeepD JDeepD closed this Nov 28, 2021
@JDeepD
Copy link
Contributor Author

JDeepD commented Nov 28, 2021

Wait I messed something while pushing

@andrei-polukhin
Copy link

andrei-polukhin commented Nov 28, 2021

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++:

/* ready[x] indicates whether the value of solve(x) has been calculated,
and if it is, value[x] contains this value.
The constant N has been chosen so that all required values fit in the arrays. */
bool ready[N];
int value[N];

int solve(int x) {
    // the function per se
    if (x < 0) return INF;
    if (x == 0) return 0;
    if (ready[x]) return value[x];
    int best = INF;
    for (auto c : coins) {
       best = min(best, solve(x-c)+1);
    }
    value[x] = best;
    ready[x] = true;
    return best;
}

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 cache is defined separately which is extremely convenient when you have several different functions which use the memoization technique.

@JDeepD
Copy link
Contributor Author

JDeepD commented Nov 28, 2021

Yeah, I also think putting the cache outside the function is not good practice.Nevertheless, I have somehow managed to mess up the commit(It disappeared from remote smh). I will make a new PR shortly...

@andrei-polukhin
Copy link

OK, I'll take a look at it then :)

@JDeepD
Copy link
Contributor Author

JDeepD commented Nov 28, 2021

Thnx. The new PR is here #5856

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants