Skip to content

Commit 1e64bf4

Browse files
atharva01903poyea
andauthored
Re-organize math/series (TheAlgorithms#5044)
* added harmonic mean * Update maths/series/harmonic_mean.py Updated the write-up of reference given in the code. Co-authored-by: John Law <[email protected]> * changes in arithmetic and geometric mean code * mean and series added in a single file Co-authored-by: John Law <[email protected]>
1 parent 08d4d22 commit 1e64bf4

File tree

3 files changed

+129
-18
lines changed

3 files changed

+129
-18
lines changed

maths/series/arithmetic_mean.py maths/series/arithmetic.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
"""
2-
ARITHMETIC MEAN : https://en.wikipedia.org/wiki/Arithmetic_mean
2+
Arithmetic mean
3+
Reference: https://en.wikipedia.org/wiki/Arithmetic_mean
34
5+
Arithmetic series
6+
Reference: https://en.wikipedia.org/wiki/Arithmetic_series
7+
(The URL above will redirect you to arithmetic progression)
48
"""
59

610

711
def is_arithmetic_series(series: list) -> bool:
812
"""
913
checking whether the input series is arithmetic series or not
10-
1114
>>> is_arithmetic_series([2, 4, 6])
1215
True
1316
>>> is_arithmetic_series([3, 6, 12, 24])
1417
False
1518
>>> is_arithmetic_series([1, 2, 3])
1619
True
20+
>>> is_arithmetic_series(4)
21+
Traceback (most recent call last):
22+
...
23+
ValueError: Input series is not valid, valid series - [2, 4, 6]
24+
>>> is_arithmetic_series([])
25+
Traceback (most recent call last):
26+
...
27+
ValueError: Input list must be a non empty list
1728
"""
29+
if not isinstance(series, list):
30+
raise ValueError("Input series is not valid, valid series - [2, 4, 6]")
31+
if len(series) == 0:
32+
raise ValueError("Input list must be a non empty list")
1833
if len(series) == 1:
1934
return True
2035
common_diff = series[1] - series[0]
@@ -37,9 +52,7 @@ def arithmetic_mean(series: list) -> float:
3752
...
3853
ValueError: Input series is not valid, valid series - [2, 4, 6]
3954
>>> arithmetic_mean([4, 8, 1])
40-
Traceback (most recent call last):
41-
...
42-
ValueError: Input list is not an arithmetic series
55+
4.333333333333333
4356
>>> arithmetic_mean([1, 2, 3])
4457
2.0
4558
>>> arithmetic_mean([])
@@ -52,8 +65,6 @@ def arithmetic_mean(series: list) -> float:
5265
raise ValueError("Input series is not valid, valid series - [2, 4, 6]")
5366
if len(series) == 0:
5467
raise ValueError("Input list must be a non empty list")
55-
if not is_arithmetic_series(series):
56-
raise ValueError("Input list is not an arithmetic series")
5768
answer = 0
5869
for val in series:
5970
answer += val

maths/series/geometric_mean.py maths/series/geometric.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
"""
2-
GEOMETRIC MEAN : https://en.wikipedia.org/wiki/Geometric_mean
2+
Geometric Mean
3+
Reference : https://en.wikipedia.org/wiki/Geometric_mean
4+
5+
Geometric series
6+
Reference: https://en.wikipedia.org/wiki/Geometric_series
37
"""
48

59

610
def is_geometric_series(series: list) -> bool:
711
"""
812
checking whether the input series is geometric series or not
9-
1013
>>> is_geometric_series([2, 4, 8])
1114
True
1215
>>> is_geometric_series([3, 6, 12, 24])
@@ -15,8 +18,19 @@ def is_geometric_series(series: list) -> bool:
1518
False
1619
>>> is_geometric_series([0, 0, 3])
1720
False
18-
21+
>>> is_geometric_series([])
22+
Traceback (most recent call last):
23+
...
24+
ValueError: Input list must be a non empty list
25+
>>> is_geometric_series(4)
26+
Traceback (most recent call last):
27+
...
28+
ValueError: Input series is not valid, valid series - [2, 4, 8]
1929
"""
30+
if not isinstance(series, list):
31+
raise ValueError("Input series is not valid, valid series - [2, 4, 8]")
32+
if len(series) == 0:
33+
raise ValueError("Input list must be a non empty list")
2034
if len(series) == 1:
2135
return True
2236
try:
@@ -44,13 +58,9 @@ def geometric_mean(series: list) -> float:
4458
...
4559
ValueError: Input series is not valid, valid series - [2, 4, 8]
4660
>>> geometric_mean([1, 2, 3])
47-
Traceback (most recent call last):
48-
...
49-
ValueError: Input list is not a geometric series
61+
1.8171205928321397
5062
>>> geometric_mean([0, 2, 3])
51-
Traceback (most recent call last):
52-
...
53-
ValueError: Input list is not a geometric series
63+
0.0
5464
>>> geometric_mean([])
5565
Traceback (most recent call last):
5666
...
@@ -61,8 +71,6 @@ def geometric_mean(series: list) -> float:
6171
raise ValueError("Input series is not valid, valid series - [2, 4, 8]")
6272
if len(series) == 0:
6373
raise ValueError("Input list must be a non empty list")
64-
if not is_geometric_series(series):
65-
raise ValueError("Input list is not a geometric series")
6674
answer = 1
6775
for value in series:
6876
answer *= value

maths/series/harmonic.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
Harmonic mean
3+
Reference: https://en.wikipedia.org/wiki/Harmonic_mean
4+
5+
Harmonic series
6+
Reference: https://en.wikipedia.org/wiki/Harmonic_series(mathematics)
7+
"""
8+
9+
10+
def is_harmonic_series(series: list) -> bool:
11+
"""
12+
checking whether the input series is arithmetic series or not
13+
>>> is_harmonic_series([ 1, 2/3, 1/2, 2/5, 1/3])
14+
True
15+
>>> is_harmonic_series([ 1, 2/3, 2/5, 1/3])
16+
False
17+
>>> is_harmonic_series([1, 2, 3])
18+
False
19+
>>> is_harmonic_series([1/2, 1/3, 1/4])
20+
True
21+
>>> is_harmonic_series([2/5, 2/10, 2/15, 2/20, 2/25])
22+
True
23+
>>> is_harmonic_series(4)
24+
Traceback (most recent call last):
25+
...
26+
ValueError: Input series is not valid, valid series - [1, 2/3, 2]
27+
>>> is_harmonic_series([])
28+
Traceback (most recent call last):
29+
...
30+
ValueError: Input list must be a non empty list
31+
>>> is_harmonic_series([0])
32+
Traceback (most recent call last):
33+
...
34+
ValueError: Input series cannot have 0 as an element
35+
>>> is_harmonic_series([1,2,0,6])
36+
Traceback (most recent call last):
37+
...
38+
ValueError: Input series cannot have 0 as an element
39+
"""
40+
if not isinstance(series, list):
41+
raise ValueError("Input series is not valid, valid series - [1, 2/3, 2]")
42+
if len(series) == 0:
43+
raise ValueError("Input list must be a non empty list")
44+
if len(series) == 1 and series[0] != 0:
45+
return True
46+
rec_series = []
47+
series_len = len(series)
48+
for i in range(0, series_len):
49+
if series[i] == 0:
50+
raise ValueError("Input series cannot have 0 as an element")
51+
rec_series.append(1 / series[i])
52+
common_diff = rec_series[1] - rec_series[0]
53+
for index in range(2, series_len):
54+
if rec_series[index] - rec_series[index - 1] != common_diff:
55+
return False
56+
return True
57+
58+
59+
def harmonic_mean(series: list) -> float:
60+
"""
61+
return the harmonic mean of series
62+
63+
>>> harmonic_mean([1, 4, 4])
64+
2.0
65+
>>> harmonic_mean([3, 6, 9, 12])
66+
5.759999999999999
67+
>>> harmonic_mean(4)
68+
Traceback (most recent call last):
69+
...
70+
ValueError: Input series is not valid, valid series - [2, 4, 6]
71+
>>> harmonic_mean([1, 2, 3])
72+
1.6363636363636365
73+
>>> harmonic_mean([])
74+
Traceback (most recent call last):
75+
...
76+
ValueError: Input list must be a non empty list
77+
78+
"""
79+
if not isinstance(series, list):
80+
raise ValueError("Input series is not valid, valid series - [2, 4, 6]")
81+
if len(series) == 0:
82+
raise ValueError("Input list must be a non empty list")
83+
answer = 0
84+
for val in series:
85+
answer += 1 / val
86+
return len(series) / answer
87+
88+
89+
if __name__ == "__main__":
90+
import doctest
91+
92+
doctest.testmod()

0 commit comments

Comments
 (0)