Can you write a Python program to check a sequence of numbers is an arithmetic progression or not? To refresh your memory, a sequence is a set of things (usually numbers) that are in order. In an Arithmetic Sequence the difference between one term and the next is a constant. In other words, we just add the same value each time. For example, the sequence 5, 7, 9, 11, 13, 15 ... is an arithmetic progression with common difference of 2. We can write an Arithmetic Sequence as a rule: xn = a + d(n−1) How would you write it using Python? Try it yourself. If you cannot figure it out, see code on the next page. def is_arithmetic(seq):
d = seq[1]  seq[0] for index in range(len(seq)  1): if not (seq[index + 1]  seq[index] == d): return False return True print(is_arithmetic([3, 7, 11, 15])) print(is_arithmetic([3, 8, 11, 15]))
0 Comments
Leave a Reply. 
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
September 2018
Categories
All
