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

Segmented sieve - doctests #9945

Merged
merged 17 commits into from
Oct 7, 2023
Merged

Conversation

quant12345
Copy link
Contributor

Describe your change:

Added doctests.

  • 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 description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Oct 6, 2023
Comment on lines 17 to 21
>>> sieve()
Traceback (most recent call last):
...
TypeError: sieve() missing 1 required positional argument: 'n'

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
>>> sieve()
Traceback (most recent call last):
...
TypeError: sieve() missing 1 required positional argument: 'n'

I think it's fine to assume that the function will be called on the right number of inputs, so I don't think this test is necessary

"""
Segmented Sieve.

Examples:
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a doctest where the input itself is a prime number. For example,

>>> sieve(29)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

This is to clarify whether the algorithm includes the input number itself or not.

Copy link
Contributor

Choose a reason for hiding this comment

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

In addition, please make sure that the function can correctly handle negative and zero inputs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tianyizheng02 corrected.

quant12345 and others added 3 commits October 7, 2023 01:55
Removed unnecessary check.
Added checks for 0 and negative numbers.
@algorithms-keeper algorithms-keeper bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Oct 6, 2023
@quant12345 quant12345 changed the base branch from master to 7782-create-subtraction-method-in-maths-folder October 6, 2023 22:10
@quant12345 quant12345 changed the base branch from 7782-create-subtraction-method-in-maths-folder to master October 6, 2023 22:11

doctest.testmod()

print(sieve(10**6))
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
print(sieve(10**6))
print(f"{sieve(10**6) = }")

@quant12345 quant12345 requested a review from cclauss October 6, 2023 22:39

"""

if n < 0:
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 be OK to combine these into a single most be a positive integer.

What happens on floating point input?

Copy link
Contributor Author

@quant12345 quant12345 Oct 6, 2023

Choose a reason for hiding this comment

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

@cclauss

print(f"{sieve(87.5) = }")

Output

temp = [True] * (high - low + 1)
TypeError: can't multiply sequence by non-int of type 'float'

Copy link
Member

Choose a reason for hiding this comment

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

Nice doctest to add.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cclauss question is off topic. For some reason, my old commits end up in pull requests. I don’t understand why this is so? Every time I make a new branch and they still get there.

Copy link
Member

Choose a reason for hiding this comment

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

I will look in a few hours…

Added float number check.
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 6, 2023
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Oct 6, 2023
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 6, 2023
@algorithms-keeper algorithms-keeper bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Oct 6, 2023
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Oct 6, 2023
@quant12345 quant12345 requested a review from cclauss October 6, 2023 23:56
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Oct 7, 2023
@cclauss cclauss enabled auto-merge (squash) October 7, 2023 07:01
Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

Nice!

@cclauss cclauss merged commit 2122474 into TheAlgorithms:master Oct 7, 2023
@quant12345
Copy link
Contributor Author

@cclauss @tianyizheng02 @dhruvmanila thank you for your patience and advice.

@quant12345 quant12345 deleted the segmented_sieve branch October 7, 2023 16:11
tianyizheng02 pushed a commit to tianyizheng02/Python that referenced this pull request Oct 8, 2023
* Replacing the generator with numpy vector operations from lu_decomposition.

* Revert "Replacing the generator with numpy vector operations from lu_decomposition."

This reverts commit ad217c6.

* Added doctests.

* Update segmented_sieve.py

Removed unnecessary check.

* Update segmented_sieve.py

Added checks for 0 and negative numbers.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update segmented_sieve.py

* Update segmented_sieve.py

Added float number check.

* Update segmented_sieve.py

* Update segmented_sieve.py

simplified verification

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update segmented_sieve.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update segmented_sieve.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* ValueError: Number 22.2 must instead be a positive integer

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
* Replacing the generator with numpy vector operations from lu_decomposition.

* Revert "Replacing the generator with numpy vector operations from lu_decomposition."

This reverts commit ad217c6.

* Added doctests.

* Update segmented_sieve.py

Removed unnecessary check.

* Update segmented_sieve.py

Added checks for 0 and negative numbers.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update segmented_sieve.py

* Update segmented_sieve.py

Added float number check.

* Update segmented_sieve.py

* Update segmented_sieve.py

simplified verification

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update segmented_sieve.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update segmented_sieve.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* ValueError: Number 22.2 must instead be a positive integer

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This PR modified some existing files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants