Skip to content

Commit 74045b6

Browse files
arithmetic_mean
1 parent 03d3435 commit 74045b6

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

maths/series/arithmetic_mean.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
def is_arithmetic_series(series: list) -> bool:
2+
"""
3+
checking whether the input series is arithmetic series or not
4+
5+
>>> is_arithmetic_series([2, 4, 6])
6+
True
7+
>>> is_arithmetic_series([3, 6, 12, 24])
8+
False
9+
>>> is_arithmetic_series([1, 2, 3])
10+
True
11+
12+
"""
13+
if len(series) == 1:
14+
return True
15+
common_diff = series[1] - series[0]
16+
for index in range(len(series) - 1):
17+
if series[index + 1] - series[index] != common_diff:
18+
return False
19+
return True
20+
21+
22+
def arithmetic_mean(series: list) -> float:
23+
"""
24+
return the geometric mean of series
25+
26+
>>> arithmetic_mean([2, 4, 6])
27+
4.0
28+
>>> arithmetic_mean([3, 6, 9, 12])
29+
7.5
30+
>>> arithmetic_mean([4, 8, 1])
31+
Traceback (most recent call last):
32+
...
33+
ValueError: Input list is not an arithmetic series
34+
>>> arithmetic_mean([1, 2, 3])
35+
2.0
36+
>>> arithmetic_mean([])
37+
Traceback (most recent call last):
38+
...
39+
ValueError: Input list must be a non empty list
40+
41+
"""
42+
if len(series) == 0:
43+
raise ValueError("Input list must be a non empty list")
44+
if not is_arithmetic_series(series):
45+
raise ValueError("Input list is not an arithmetic series")
46+
answer = 0
47+
for _ in series:
48+
answer += _
49+
return answer / len(series)
50+
51+
52+
if __name__ == "__main__":
53+
import doctest
54+
55+
doctest.testmod()

0 commit comments

Comments
 (0)