Skip to content

Commit 885a3b9

Browse files
committed
fix(maths): validate input parameters and add doctests for harmonic progression
1 parent e1a2a8b commit 885a3b9

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

‎maths/sum_of_harmonic_series.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@ def sum_of_harmonic_progression(
1212
0.75
1313
>>> sum_of_harmonic_progression(1 / 5, 5, 5)
1414
0.45666666666666667
15+
>>> sum_of_harmonic_progression(0,2,5)
16+
Traceback (most recent call last):
17+
...
18+
ValueError: first_term cannot be zero
19+
>>> sum_of_harmonic_progression(1/2,2,0)
20+
Traceback (most recent call last):
21+
...
22+
ValueError: number_of_terms must be greater than zero
1523
"""
24+
if first_term == 0:
25+
raise ValueError("first_term cannot be zero")
26+
if number_of_terms <= 0:
27+
raise ValueError("number_of_terms must be greater than zero")
1628
arithmetic_progression = [1 / first_term]
1729
first_term = 1 / first_term
1830
for _ in range(number_of_terms - 1):

0 commit comments

Comments
 (0)