How to check if a sequence of numbers is an Arithmetic progression (consecutive differences are the same) in Python

1 Answer

0 votes
def is_arithmetic_progression(lst):
    size = len(lst)
    if size == 1:
        return True

    lst.sort()

    difference = lst[1] - lst[0]
    for i in range(2, size):
        if lst[i] - lst[i - 1] != difference:
            return False

    return True

lst = [10, 20, 15, 5, 25, 35, 30]
    
print("Yes" if is_arithmetic_progression(lst) else "No")



'''
run:

Yes

'''

 



answered Sep 21, 2024 by avibootz
...