Skip to content

Power using recursion: correctly implemented recursion #5050

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 3 commits into from
Closed

Power using recursion: correctly implemented recursion #5050

wants to merge 3 commits into from

Conversation

matssson
Copy link

@matssson matssson commented Oct 5, 2021

Describe your change:

We make the recursion many times faster by calculating (base*base)^exponent/2 instead of base * base ^ (exponent-1).
We also throw a valueerror for 0^0.

  • 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}.

@matssson matssson requested a review from Kush1101 as a code owner October 5, 2021 18:09
@ghost
Copy link

ghost commented Oct 5, 2021

Multiple Pull Request Detected

@matssson, we are extremely excited that you want to submit multiple algorithms in this repository but we have a limit on how many pull request a user can keep open at a time. This is to make sure all maintainers and users focus on a limited number of pull requests at a time to maintain the quality of the code.

This pull request is being closed as the user already has an open pull request. Please focus on your previous pull request before opening another one. Thank you for your cooperation.

User opened pull requests (including this one): #5050, #5048, #5045, #5043

@ghost ghost added the awaiting reviews This PR is ready to be reviewed label Oct 5, 2021
@ghost ghost closed this Oct 5, 2021
@ghost ghost removed the request for review from Kush1101 October 5, 2021 18:09
@poyea poyea reopened this Oct 5, 2021
@ghost ghost added enhancement This PR modified some existing files tests are failing Do not merge until tests pass labels Oct 5, 2021
@matssson
Copy link
Author

matssson commented Oct 5, 2021

The test failed since we threw a ValueError for 0^0, where it compared it to pythons pow function, which returns 1.
0^0 now returns 1 and passes all tests.

@ghost ghost removed the tests are failing Do not merge until tests pass label Oct 6, 2021
@@ -13,7 +13,7 @@
"""


def power(base: int, exponent: int) -> float:
def power(base: int, exponent: 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.

Please add a test for 0^0?

Copy link
Author

Choose a reason for hiding this comment

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

But I removed the check for 0^0 since the testing suite expected it to return 1, which math.pow does

Copy link
Member

@poyea poyea Oct 6, 2021

Choose a reason for hiding this comment

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

You may reference those fast exp in the same folder and see what are their behaviour. It's still ok to add tests for this function.

@@ -23,7 +23,14 @@ def power(base: int, exponent: int) -> float:
... for base in range(-10, 10) for exponent in range(10))
True
"""
return base * power(base, (exponent - 1)) if exponent else 1
Copy link
Member

Choose a reason for hiding this comment

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

I think this works for 0^0.

Copy link
Member

@poyea poyea left a comment

Choose a reason for hiding this comment

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

@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 Oct 6, 2021
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting changes A maintainer has requested changes to this PR enhancement This PR modified some existing files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants