Skip to content

Commit a984652

Browse files
Rohanrbharadwajcclauss
andauthoredNov 7, 2021
[mypy] Fix type annotations for maths directory (TheAlgorithms#5782)
* [mypy] Fix annotations in `maths/series/p_series.py` * Update p_series.py * Update p_series.py * Remove from excluded in mypy.ini * Type annotation for series * Annotate maths/proth_number.py (properly) * Remove from excluded in mypy.ini * Annotate average_mode.py * Update average_mode.py * Update average_mode.py * Update average_mode.py * Update average_mode.py * Remove from excluded in mypy.ini * Fix annotations in gamma_recursive.py * Remove from excluded in mypy.ini * Annotations for geometric_series.py * Update geometric_series.py * Update mypy.ini * Update average_mode.py * Update average_mode.py * Update average_mode.py * Update mypy.ini * Update mypy.ini * Update mypy.ini * Update average_mode.py * Update proth_number.py * Update average_mode.py * Update gamma_recursive.py * Update proth_number.py * Update mypy.ini * Update geometric_series.py * Update average_mode.py * Update proth_number.py * Update geometric_series.py * Update geometric_series.py * Update geometric_series.py * Update p_series.py * Update geometric_series.py * Update p_series.py * Update p_series.py * Update geometric_series.py * Update p_series.py * Update p_series.py * Remove data_structures/stacks/next_greater_element.py| Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent db5aa1d commit a984652

File tree

6 files changed

+66
-61
lines changed

6 files changed

+66
-61
lines changed
 

‎maths/average_mode.py

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
1-
def mode(input_list: list) -> list: # Defining function "mode."
1+
from typing import Any
2+
3+
4+
def mode(input_list: list) -> list[Any]:
25
"""This function returns the mode(Mode as in the measures of
36
central tendency) of the input data.
47
58
The input list may contain any Datastructure or any Datatype.
69
7-
>>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2]
8-
>>> mode(input_list)
10+
>>> mode([2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2])
911
[2]
10-
>>> input_list = [3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 2, 2, 2]
11-
>>> mode(input_list)
12+
>>> mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 2, 2, 2])
1213
[2]
13-
>>> input_list = [3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 4, 2, 2, 4, 2]
14-
>>> mode(input_list)
14+
>>> mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 4, 2, 2, 4, 2])
1515
[2, 4]
16-
>>> input_list = ["x", "y", "y", "z"]
17-
>>> mode(input_list)
16+
>>> mode(["x", "y", "y", "z"])
1817
['y']
19-
>>> input_list = ["x", "x" , "y", "y", "z"]
20-
>>> mode(input_list)
18+
>>> mode(["x", "x" , "y", "y", "z"])
2119
['x', 'y']
2220
"""
23-
result = list() # Empty list to store the counts of elements in input_list
24-
for x in input_list:
25-
result.append(input_list.count(x))
26-
if not result:
21+
if not input_list:
2722
return []
28-
y = max(result) # Gets the maximum value in the result list.
23+
result = [input_list.count(value) for value in input_list]
24+
y = max(result) # Gets the maximum count in the input list.
2925
# Gets values of modes
30-
result = {input_list[i] for i, value in enumerate(result) if value == y}
31-
return sorted(result)
26+
return sorted({input_list[i] for i, value in enumerate(result) if value == y})
3227

3328

3429
if __name__ == "__main__":

‎maths/gamma_recursive.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Gamma function is a very useful tool in math and physics.
33
It helps calculating complex integral in a convenient way.
44
for more info: https://en.wikipedia.org/wiki/Gamma_function
5-
65
Python's Standard Library math.gamma() function overflows around gamma(171.624).
76
"""
87
from math import pi, sqrt
@@ -71,7 +70,7 @@ def test_gamma() -> None:
7170
from doctest import testmod
7271

7372
testmod()
74-
num = 1
73+
num = 1.0
7574
while num:
7675
num = float(input("Gamma of: "))
7776
print(f"gamma({num}) = {gamma(num)}")

‎maths/proth_number.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""
22
Calculate the nth Proth number
3-
43
Source:
54
https://handwiki.org/wiki/Proth_number
65
"""
@@ -12,22 +11,17 @@ def proth(number: int) -> int:
1211
"""
1312
:param number: nth number to calculate in the sequence
1413
:return: the nth number in Proth number
15-
1614
Note: indexing starts at 1 i.e. proth(1) gives the first Proth number of 3
17-
1815
>>> proth(6)
1916
25
20-
2117
>>> proth(0)
2218
Traceback (most recent call last):
2319
...
2420
ValueError: Input value of [number=0] must be > 0
25-
2621
>>> proth(-1)
2722
Traceback (most recent call last):
2823
...
2924
ValueError: Input value of [number=-1] must be > 0
30-
3125
>>> proth(6.0)
3226
Traceback (most recent call last):
3327
...
@@ -44,14 +38,12 @@ def proth(number: int) -> int:
4438
elif number == 2:
4539
return 5
4640
else:
47-
block_index = number // 3
4841
"""
4942
+1 for binary starting at 0 i.e. 2^0, 2^1, etc.
5043
+1 to start the sequence at the 3rd Proth number
5144
Hence, we have a +2 in the below statement
5245
"""
53-
block_index = math.log(block_index, 2) + 2
54-
block_index = int(block_index)
46+
block_index = int(math.log(number // 3, 2)) + 2
5547

5648
proth_list = [3, 5]
5749
proth_index = 2
@@ -66,6 +58,10 @@ def proth(number: int) -> int:
6658

6759

6860
if __name__ == "__main__":
61+
import doctest
62+
63+
doctest.testmod()
64+
6965
for number in range(11):
7066
value = 0
7167
try:

‎maths/series/geometric_series.py

+28-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
This is a pure Python implementation of the Geometric Series algorithm
33
https://en.wikipedia.org/wiki/Geometric_series
4-
54
Run the doctests with the following command:
65
python3 -m doctest -v geometric_series.py
76
or
@@ -11,24 +10,33 @@
1110
"""
1211

1312

14-
def geometric_series(nth_term: int, start_term_a: int, common_ratio_r: int) -> list:
15-
"""Pure Python implementation of Geometric Series algorithm
13+
from __future__ import annotations
14+
15+
16+
def geometric_series(
17+
nth_term: float | int,
18+
start_term_a: float | int,
19+
common_ratio_r: float | int,
20+
) -> list[float | int]:
21+
"""
22+
Pure Python implementation of Geometric Series algorithm
23+
1624
:param nth_term: The last term (nth term of Geometric Series)
1725
:param start_term_a : The first term of Geometric Series
1826
:param common_ratio_r : The common ratio between all the terms
1927
:return: The Geometric Series starting from first term a and multiple of common
2028
ration with first term with increase in power till last term (nth term)
2129
Examples:
2230
>>> geometric_series(4, 2, 2)
23-
[2, '4.0', '8.0', '16.0']
31+
[2, 4.0, 8.0, 16.0]
2432
>>> geometric_series(4.0, 2.0, 2.0)
25-
[2.0, '4.0', '8.0', '16.0']
33+
[2.0, 4.0, 8.0, 16.0]
2634
>>> geometric_series(4.1, 2.1, 2.1)
27-
[2.1, '4.41', '9.261000000000001', '19.448100000000004']
35+
[2.1, 4.41, 9.261000000000001, 19.448100000000004]
2836
>>> geometric_series(4, 2, -2)
29-
[2, '-4.0', '8.0', '-16.0']
37+
[2, -4.0, 8.0, -16.0]
3038
>>> geometric_series(4, -2, 2)
31-
[-2, '-4.0', '-8.0', '-16.0']
39+
[-2, -4.0, -8.0, -16.0]
3240
>>> geometric_series(-4, 2, 2)
3341
[]
3442
>>> geometric_series(0, 100, 500)
@@ -38,26 +46,30 @@ def geometric_series(nth_term: int, start_term_a: int, common_ratio_r: int) -> l
3846
>>> geometric_series(0, 0, 0)
3947
[]
4048
"""
41-
if "" in (nth_term, start_term_a, common_ratio_r):
42-
return ""
43-
series = []
49+
if not all((nth_term, start_term_a, common_ratio_r)):
50+
return []
51+
series: list[float | int] = []
4452
power = 1
4553
multiple = common_ratio_r
4654
for _ in range(int(nth_term)):
4755
if series == []:
4856
series.append(start_term_a)
4957
else:
5058
power += 1
51-
series.append(str(float(start_term_a) * float(multiple)))
59+
series.append(float(start_term_a * multiple))
5260
multiple = pow(float(common_ratio_r), power)
5361
return series
5462

5563

5664
if __name__ == "__main__":
57-
nth_term = input("Enter the last number (n term) of the Geometric Series")
58-
start_term_a = input("Enter the starting term (a) of the Geometric Series")
59-
common_ratio_r = input(
60-
"Enter the common ratio between two terms (r) of the Geometric Series"
65+
import doctest
66+
67+
doctest.testmod()
68+
69+
nth_term = float(input("Enter the last number (n term) of the Geometric Series"))
70+
start_term_a = float(input("Enter the starting term (a) of the Geometric Series"))
71+
common_ratio_r = float(
72+
input("Enter the common ratio between two terms (r) of the Geometric Series")
6173
)
6274
print("Formula of Geometric Series => a + ar + ar^2 ... +ar^n")
6375
print(geometric_series(nth_term, start_term_a, common_ratio_r))

‎maths/series/p_series.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,52 @@
11
"""
22
This is a pure Python implementation of the P-Series algorithm
33
https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#P-series
4-
54
For doctests run following command:
65
python -m doctest -v p_series.py
76
or
87
python3 -m doctest -v p_series.py
9-
108
For manual testing run:
119
python3 p_series.py
1210
"""
1311

1412

15-
def p_series(nth_term: int, power: int) -> list:
16-
"""Pure Python implementation of P-Series algorithm
13+
from __future__ import annotations
1714

18-
:return: The P-Series starting from 1 to last (nth) term
1915

16+
def p_series(nth_term: int | float | str, power: int | float | str) -> list[str]:
17+
"""
18+
Pure Python implementation of P-Series algorithm
19+
:return: The P-Series starting from 1 to last (nth) term
2020
Examples:
2121
>>> p_series(5, 2)
22-
[1, '1/4', '1/9', '1/16', '1/25']
22+
['1', '1 / 4', '1 / 9', '1 / 16', '1 / 25']
2323
>>> p_series(-5, 2)
2424
[]
2525
>>> p_series(5, -2)
26-
[1, '1/0.25', '1/0.1111111111111111', '1/0.0625', '1/0.04']
26+
['1', '1 / 0.25', '1 / 0.1111111111111111', '1 / 0.0625', '1 / 0.04']
2727
>>> p_series("", 1000)
28-
''
28+
['']
2929
>>> p_series(0, 0)
3030
[]
3131
>>> p_series(1, 1)
32-
[1]
32+
['1']
3333
"""
3434
if nth_term == "":
35-
return nth_term
35+
return [""]
3636
nth_term = int(nth_term)
3737
power = int(power)
38-
series = []
38+
series: list[str] = []
3939
for temp in range(int(nth_term)):
40-
series.append(f"1/{pow(temp + 1, int(power))}" if series else 1)
40+
series.append(f"1 / {pow(temp + 1, int(power))}" if series else "1")
4141
return series
4242

4343

4444
if __name__ == "__main__":
45-
nth_term = input("Enter the last number (nth term) of the P-Series")
46-
power = input("Enter the power for P-Series")
45+
import doctest
46+
47+
doctest.testmod()
48+
49+
nth_term = int(input("Enter the last number (nth term) of the P-Series"))
50+
power = int(input("Enter the power for P-Series"))
4751
print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p")
4852
print(p_series(nth_term, power))

‎mypy.ini

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
ignore_missing_imports = True
33
install_types = True
44
non_interactive = True
5-
exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|maths/average_mode.py|maths/gamma_recursive.py|maths/proth_number.py|maths/series/geometric_series.py|maths/series/p_series.py|matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py)
6-
5+
exclude = (graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py)

0 commit comments

Comments
 (0)
Please sign in to comment.