In: Computer Science
The centered average of a list of numbers is the arithmetic mean of all the numbers in the list, except for the smallest and largest values. If the list has multiple copies of the smallest or largest values, only one copy is ignored when calculating the mean. For example, given the list [1, 1, 5, 3 5, 10, 8, 7], one 1 and 10 are ignored, and the centered average is the mean of the remaining values; that is, 5.2. Use the function design recipe to develop a function named centered_average. The function takes a list of integers. (Assume that the list has at least three integers). The function returns the centered average of the list. Hint: In this exercise you are permitted and encouraged to use Python's min and max functions.
**** YOU CANNOT USE THE FOLLOWING:
list slicing (e.g., lst[i : j] or (lst[i : j] = t) the del statement (e.g., del lst[0]) Python's built-in reversed and sorted functions. any of the Python methods that provide list operations; e.g., append, clear , copy , count, extend, index, insert, pop, remove, reverse and sort list comprehensions
Code and output
Code for copying
def centered_average(list1):
s=0
count=0
for i in list1:
s+=i
count+=1
s-=min(list1)
s-=max(list1)
count=count-2
return s/count
list1=list(map(int,input("Enter the list :
").rstrip().split()))
print("The centered average is
{}".format(centered_average(list1)))
Code snippet
def centered_average(list1):
s=0
count=0
for i in list1:
s+=i
count+=1
s-=min(list1)
s-=max(list1)
count=count-2
return s/count
list1=list(map(int,input("Enter the list : ").rstrip().split()))
print("The centered average is {}".format(centered_average(list1)))