I have a function that takes a list and removes (one instance of) the smallest and largest elements, then takes the average of the remaining elements. Running it doesnt bring up any errors, although on checking the results I realised they were incorrect. Here's the program:
def centered_average(x):
x.sort()
y = 0
for i in x:
if x.index(i) == 0 or x.index(i) == (len(x)-1):
print(i, "is being removed")
x.remove(i)
i +=1
else:
y += i
print(i, "is being added")
return (y / len(x))
def average(x):
return sum(x)/len(x)
(the print functions were put in for checking)
On putting through a list of
x = [1,2,3,4,5]
the result was:
1 is being removed
3 is being added
4 is being added
5 is being removed
2.3333333333333335
therefore, we can assume x[1] is not being used in the function, and I would like to know why.
Thanks in advance for any help.