MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / is_arithmetic_series

Function is_arithmetic_series

maths/series/arithmetic.py:11–39  ·  view source on GitHub ↗

checking whether the input series is arithmetic series or not >>> is_arithmetic_series([2, 4, 6]) True >>> is_arithmetic_series([3, 6, 12, 24]) False >>> is_arithmetic_series([1, 2, 3]) True >>> is_arithmetic_series(4) Traceback (most recent call last): .

(series: list)

Source from the content-addressed store, hash-verified

9
10
11def is_arithmetic_series(series: list) -> bool:
12 """
13 checking whether the input series is arithmetic series or not
14 >>> is_arithmetic_series([2, 4, 6])
15 True
16 >>> is_arithmetic_series([3, 6, 12, 24])
17 False
18 >>> is_arithmetic_series([1, 2, 3])
19 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
28 """
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")
33 if len(series) == 1:
34 return True
35 common_diff = series[1] - series[0]
36 for index in range(len(series) - 1):
37 if series[index + 1] - series[index] != common_diff:
38 return False
39 return True
40
41
42def arithmetic_mean(series: list) -> float:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected