Skip to content

Add catalan_numbers.py #4455

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

Merged

Conversation

Kommandat
Copy link
Contributor

@Kommandat Kommandat commented May 26, 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}.

Additional Notes

Excited to make my first contribution to The Algorithms! Catalan numbers are a sequence in mathematics that represent answers to many combinatorics problems. They can be calculated using a recurrence relation and we can use tabulation (similarly to how we can for the Fibonacci sequence). I thought this best fit in the "dynamic_programming" folder given we're using tabulation to compute the sequence.

All work is my own, but I did take a look at the existing C++ Catalan number algorithm and Python Fibonacci algorithm for inspiration and best practices. If this is approved, I hope to contribute to Algorithms-Explanation as well.

Please let me know if you have any feedback!

Sources:
[1] https://brilliant.org/wiki/catalan-numbers/
[2] https://en.wikipedia.org/wiki/Catalan_number

@Kommandat Kommandat marked this pull request as ready for review May 26, 2021 17:30
@Kommandat Kommandat requested a review from Kush1101 as a code owner May 26, 2021 17:30
@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 type hints https://docs.python.org/3/library/typing.html labels May 26, 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.

@Kommandat Kommandat force-pushed the lakshay/add_catalan_numbers branch from c018648 to f071d62 Compare May 26, 2021 17:38
@ghost ghost removed the require type hints https://docs.python.org/3/library/typing.html label May 26, 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.

@Kommandat Kommandat force-pushed the lakshay/add_catalan_numbers branch from f071d62 to cf9a272 Compare May 26, 2021 17:40
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.



class Catalan:
def __init__(self, N: int) -> None:
Copy link

Choose a reason for hiding this comment

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

Please provide descriptive name for the parameter: N

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a comment explaining N's function below this line and I don't think a more descriptive variable name is necessary

@Kommandat Kommandat force-pushed the lakshay/add_catalan_numbers branch from cf9a272 to c694ed1 Compare May 27, 2021 16:06
@ghost ghost removed the require descriptive names This PR needs descriptive function and/or variable names label May 27, 2021
@appgurueu
Copy link

I don't like the unneeded OOP.

Copy link
Member

@mrmaxguns mrmaxguns left a comment

Choose a reason for hiding this comment

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

Hello. Thanks for your contribution. I performed a review. If you disagree with the changes I suggested, feel free to explain your disagreement by commenting on the review.

Mostly, I suggested changes to avoid unwanted side-effects. These changes are based on the following contribution guideline:

have minimal side effects (Ex. print(), plot(), read(), write()).

Also, when the user enters erroneous input, an exception should be raised, instead of it simply being printed (this applies only to the algorithm itself, not in the if __name__ == "__main__" block).

raise Python exceptions (ValueError, etc.) on erroneous input values

)
elif upper_limit == 0:
self.catalan_numbers[0] = 1
print(self.catalan_numbers)
Copy link
Member

Choose a reason for hiding this comment

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

I would avoid print statements in the algorithm itself as they produce unwanted side-effects.

"""
if sequence_no is not None:
if sequence_no < len(self.catalan_numbers):
return print(self.catalan_numbers[: sequence_no + 1])
Copy link
Member

Choose a reason for hiding this comment

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

This should return the value directly for the same reason described above. This is where the algorithm produces its output, so the output should be directly returned.

if sequence_no < len(self.catalan_numbers):
return print(self.catalan_numbers[: sequence_no + 1])
else:
print("Out of bound.")
Copy link
Member

Choose a reason for hiding this comment

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

This is an error, so an exception should be raised here.

else:
print("Out of bound.")
else:
print("Please specify a value")
Copy link
Member

Choose a reason for hiding this comment

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

Again, this should be refactored as an exception.

@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 May 29, 2021
@ghost ghost added require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html awaiting reviews This PR is ready to be reviewed and removed awaiting changes A maintainer has requested changes to this PR labels May 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.

@Kommandat Kommandat requested a review from l3str4nge as a code owner May 31, 2021 21:05
@ghost ghost removed require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels May 31, 2021
@Kommandat Kommandat force-pushed the lakshay/add_catalan_numbers branch 3 times, most recently from 7e15dd5 to bb62754 Compare May 31, 2021 21:10
@Kommandat Kommandat force-pushed the lakshay/add_catalan_numbers branch from bb62754 to 7f5578d Compare May 31, 2021 21:15
@Kommandat
Copy link
Contributor Author

Hello. Thanks for your contribution. I performed a review. If you disagree with the changes I suggested, feel free to explain your disagreement by commenting on the review.

Mostly, I suggested changes to avoid unwanted side-effects. These changes are based on the following contribution guideline:

have minimal side effects (Ex. print(), plot(), read(), write()).

Also, when the user enters erroneous input, an exception should be raised, instead of it simply being printed (this applies only to the algorithm itself, not in the if __name__ == "__main__" block).

raise Python exceptions (ValueError, etc.) on erroneous input values

@mrmaxguns Thanks for the review! I agree with all your comments and made the changes you recommended. I requested a re-review, please take another look when you get a chance.

As an aside, I based much of my code on the Fibonacci algorithm in the same folder thinking an existing algorithm already followed the guidelines but I was mistaken. It would nice to add some examples to the contribution guidelines that demonstrate best practices so others don't do what I did.

Once again, thanks!

@Kommandat Kommandat requested a review from mrmaxguns May 31, 2021 21:23
Copy link
Member

@mrmaxguns mrmaxguns left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution! Yes, indeed, some code in this repository does not follow the contribution guidelines, as they may have changed or the code was improperly reviewed. If you notice such code, feel free to open a pull request addressing the changes.

Regarding the code, it looks good. Thanks for applying the suggestions! This should be merged once tests are run and I double check the code for any issues I might not have caught now.

@ghost ghost removed the awaiting reviews This PR is ready to be reviewed label May 31, 2021
@mrmaxguns mrmaxguns added awaiting merge This PR is approved and ready to be merged and removed algorithmic functions should not print labels Jun 1, 2021
@mrmaxguns
Copy link
Member

Thank you for your contribution! 💯

@mrmaxguns mrmaxguns merged commit cb0a548 into TheAlgorithms:master Jun 1, 2021
@ghost ghost removed the awaiting merge This PR is approved and ready to be merged label Jun 1, 2021
shermanhui pushed a commit to shermanhui/Python that referenced this pull request Oct 22, 2021
Reviewed by @mrmaxguns. This is an implementation of Catalan Numbers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants