-
-
Notifications
You must be signed in to change notification settings - Fork 46.5k
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
Conversation
maths/segmented_sieve.py
Outdated
>>> sieve() | ||
Traceback (most recent call last): | ||
... | ||
TypeError: sieve() missing 1 required positional argument: 'n' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
>>> 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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tianyizheng02 corrected.
Removed unnecessary check.
Added checks for 0 and negative numbers.
for more information, see https://pre-commit.ci
maths/segmented_sieve.py
Outdated
|
||
doctest.testmod() | ||
|
||
print(sieve(10**6)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
print(sieve(10**6)) | |
print(f"{sieve(10**6) = }") |
maths/segmented_sieve.py
Outdated
|
||
""" | ||
|
||
if n < 0: |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
print(f"{sieve(87.5) = }")
Output
temp = [True] * (high - low + 1)
TypeError: can't multiply sequence by non-int of type 'float'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice doctest to add.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
simplified verification
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
@cclauss @tianyizheng02 @dhruvmanila thank you for your patience and advice. |
* 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>
* 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>
Describe your change:
Added doctests.
Checklist: