Re-organize math/series (#5044) · zinating/algorithms-python@1e64bf4 · GitHub
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 <johnlaw.po@gmail.com> * changes in arithmetic and geometric mean code * mean and series added in a single file Co-authored-by: John Law <johnlaw.po@gmail.com>
1 parent 08d4d22 commit 1e64bf4

3 files changed

Lines changed: 129 additions & 18 deletions

File tree

Lines changed: 18 additions & 7 deletions
Lines changed: 19 additions & 11 deletions
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

Lines changed: 92 additions & 0 deletions

0 commit comments

Comments
 (0)