-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
[mypy] Fix type annotations for maths directory #5782
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
Changes from all commits
e34dd4d
cea397e
7173166
b5440ec
1ce55e7
95e41dd
4f00c09
99a0cf5
e9c81ab
d2a04cf
567210b
6b07a1f
175b785
9902c88
e52a293
e98c58e
83af001
dc86c78
33b0977
9bbace1
bbfcc6e
dbc81e9
289c029
9f7c657
d384199
db95b64
ae56fe9
b610e7f
882a12c
d645da0
d112ce4
dd1ff0a
c416d74
a9d9c1e
85b0ba8
5078821
6bf7b4c
8da52be
db01780
2e5cafb
7508c25
e8cb833
bdb576d
a362ea5
f4597b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,52 @@ | ||
""" | ||
This is a pure Python implementation of the P-Series algorithm | ||
https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#P-series | ||
|
||
For doctests run following command: | ||
python -m doctest -v p_series.py | ||
or | ||
python3 -m doctest -v p_series.py | ||
|
||
For manual testing run: | ||
python3 p_series.py | ||
""" | ||
|
||
|
||
def p_series(nth_term: int, power: int) -> list: | ||
"""Pure Python implementation of P-Series algorithm | ||
from __future__ import annotations | ||
|
||
:return: The P-Series starting from 1 to last (nth) term | ||
|
||
def p_series(nth_term: int | float | str, power: int | float | str) -> list[str]: | ||
""" | ||
Pure Python implementation of P-Series algorithm | ||
:return: The P-Series starting from 1 to last (nth) term | ||
Examples: | ||
>>> p_series(5, 2) | ||
[1, '1/4', '1/9', '1/16', '1/25'] | ||
['1', '1 / 4', '1 / 9', '1 / 16', '1 / 25'] | ||
>>> p_series(-5, 2) | ||
[] | ||
>>> p_series(5, -2) | ||
[1, '1/0.25', '1/0.1111111111111111', '1/0.0625', '1/0.04'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like a cooler test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make all strings here instead of floats, then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation is tolerant of both. We should not lose that flexibility. Our type hint says that we expect float but our implementation is flexible to do the right thing if someone hands us something that is not a float but is still reasonable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://pydantic-docs.helpmanual.io does a nice job of this. |
||
['1', '1 / 0.25', '1 / 0.1111111111111111', '1 / 0.0625', '1 / 0.04'] | ||
>>> p_series("", 1000) | ||
'' | ||
[''] | ||
>>> p_series(0, 0) | ||
[] | ||
>>> p_series(1, 1) | ||
[1] | ||
['1'] | ||
""" | ||
if nth_term == "": | ||
return nth_term | ||
return [""] | ||
nth_term = int(nth_term) | ||
power = int(power) | ||
series = [] | ||
series: list[str] = [] | ||
for temp in range(int(nth_term)): | ||
series.append(f"1/{pow(temp + 1, int(power))}" if series else 1) | ||
series.append(f"1 / {pow(temp + 1, int(power))}" if series else "1") | ||
return series | ||
|
||
|
||
if __name__ == "__main__": | ||
nth_term = input("Enter the last number (nth term) of the P-Series") | ||
power = input("Enter the power for P-Series") | ||
import doctest | ||
|
||
doctest.testmod() | ||
|
||
nth_term = int(input("Enter the last number (nth term) of the P-Series")) | ||
power = int(input("Enter the power for P-Series")) | ||
print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p") | ||
print(p_series(nth_term, power)) |
Uh oh!
There was an error while loading. Please reload this page.