I will like to find out the numbers in an array which is equal to the arithmetic mean of their immediate neighbors.The twist here is to use 0 if a[i-1] or a[i+1] does not exist.
For example:
a = [2,4,6,8,2,5]
a[0] = (0 + 4)/2 = 2 ----count 1
a[1] = (2 + 6)/2 = 4---- count 2
a[5] = (2 + 0)/2 != 5 ----no count
If it is not, it should be a count of 0. I have written the below code but it won't work for all the arrays, like this one. How can I do it?
a = [100, 110, 120, 130, 140, 150, 160, 170, 180, 190]
My code:
def countMeans(a):
count = 0
for i in range(1, len(a)-1):
if a[i-1]+ a[i+1] is 2 * a[i]:
count += 1
return count