1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-09 19:19:07 +01:00
CodeWars/6kyu/find_the_missing_term_in_arith_prog/solution.py

18 lines
432 B
Python
Raw Normal View History

def find_missing(sequence):
# Relation => a_n - a_(n-1) = d
diffs = []
for i in range(len(sequence) - 1):
diffs.append(sequence[i + 1] - sequence[i])
max_diff = diffs[0]
for index, diff in enumerate(diffs[1:], 1):
if abs(diff) > abs(max_diff):
return sequence[index] + max_diff
elif abs(max_diff) > abs(diff):
return sequence[0] + diff
raise ValueError()