Skip to content
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

BST and RSA doctest #8693

Merged
merged 13 commits into from
Aug 15, 2023
Merged

BST and RSA doctest #8693

merged 13 commits into from
Aug 15, 2023

Conversation

isidroas
Copy link
Contributor

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 include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Apr 26, 2023
@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed and removed tests are failing Do not merge until tests pass labels Apr 26, 2023
Copy link

@ankit-gautam23 ankit-gautam23 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@rohan472000 rohan472000 left a comment

Choose a reason for hiding this comment

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

Few Suggestions :-

  1. Handle potential errors: Currently, if there are any errors during the key file generation, the program will terminate abruptly. It would be helpful to handle potential errors more gracefully by catching exceptions and providing appropriate error messages to the user.

  2. Consider using a different random number generator: The code uses the random module for generating random numbers. In cryptography, it's generally recommended to use a cryptographically secure random number generator (CSPRNG) for generating cryptographic keys. Consider using a CSPRNG from a cryptographic library such as secrets module in Python or cryptography library.

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Jun 3, 2023
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Jun 3, 2023
@isidroas
Copy link
Contributor Author

isidroas commented Jun 3, 2023

Hi @rohan472000, thanks for the review.

  1. I just added 4a13d5d which raises an error when trying to delete a non-existent element in a Binary Search Tree. As for the generate_key function, it only has the size argument and I don't see any interesting potential error.
  2. I discovered that the secrets module doesn't have have seed method. That would be important for doctests. Also, I thought that the main goal this project is not to use it in production code, but to teach algorithms in a way that it is even easier than reading Wikipedia or a book.

data_structures/binary_tree/binary_search_tree.py Outdated Show resolved Hide resolved
data_structures/binary_tree/binary_search_tree.py Outdated Show resolved Hide resolved
Comment on lines +1 to 60
r"""
A binary search Tree

Example
8
/ \
3 10
/ \ \
1 6 14
/ \ /
4 7 13

>>> t = BinarySearchTree()
>>> t.insert(8, 3, 6, 1, 10, 14, 13, 4, 7)
>>> print(" ".join(repr(i.value) for i in t.traversal_tree()))
8 3 1 6 4 7 10 14 13
>>> print(" ".join(repr(i.value) for i in t.traversal_tree(postorder)))
1 4 7 6 3 13 14 10 8
>>> t.remove(20)
Traceback (most recent call last):
...
ValueError: Value 20 not found
>>> BinarySearchTree().search(6)
Traceback (most recent call last):
...
IndexError: Warning: Tree is empty! please use another.

Other example:

>>> testlist = (8, 3, 6, 1, 10, 14, 13, 4, 7)
>>> t = BinarySearchTree()
>>> for i in testlist:
... t.insert(i)

Prints all the elements of the list in order traversal
>>> print(t)
{'8': ({'3': (1, {'6': (4, 7)})}, {'10': (None, {'14': (13, None)})})}

Test existence
>>> t.search(6) is not None
True
>>> t.search(-1) is not None
False

>>> t.search(6).is_right
True
>>> t.search(1).is_right
False

>>> t.get_max().value
14
>>> t.get_min().value
1
>>> t.empty()
False
>>> for i in testlist:
... t.remove(i)
>>> t.empty()
True
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if doctest.testmod will run these doctests since they're not contained in any function or class. @cclauss Where should these examples go?

isidroas and others added 4 commits August 14, 2023 20:39
Also use 'is' instead of '=='

Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
@tianyizheng02 tianyizheng02 merged commit efaf526 into TheAlgorithms:master Aug 15, 2023
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Aug 15, 2023
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
* rsa key doctest

* move doctest to module docstring

* all tests to doctest

* moved is_right to property

* is right test

* fixed rsa doctest import

* Test error when deleting non-existing element

* fixing ruff EM102

* convert property 'is_right' to one-liner

Also use 'is' instead of '=='

Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>

* child instead of children

Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>

* remove type hint

* Update data_structures/binary_tree/binary_search_tree.py

---------

Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
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.

4 participants