Answer:
from functools import reduce
def mean(mylist):
score = reduce(lambda x,y: x + y, mylist)/ len(mylist)
return score
def median(mylist):
sorted(mylist)
list_len = len(mylist) % 2
i = round(len(mylist)/2)
x = len(mylist)//2
if list_len == 0:
median = (mylist[x] + mylist[x+1]) / 2
else:
median = mylist[i]
return median
def mode(mylist):
unique = set(mylist)
unique = list(unique)
collector = [mylist.count(key) for key in unique]
maxi = max(collector)
loc = collector.index(maxi)
return unique[loc]
def main():
scores = input( 'Enter list of numbers: ').split(",")
scores = [int(score) for score in scores]
operation = input('Enter operation: ')
operator = ['mean', 'median', 'mode']
for x in iter(list, 0):
if operation in operator:
break
print("Invalid operation: ")
operation = input('Enter operation')
index_loc = operator.index(operation)
if index_loc == 0:
return mean(scores)
elif index_loc == 1:
return median(scores)
#return np.median(scores) can be used of the defined function
elif index_loc == 2:
#return stats.mode(scores)[0] can be used of the defined function
return mode(scores)
print( main( ) )
Explanation:
The main python function calls conditionally three statistical functions namely mean, median and mode. It prompts for user input for a list of integer numbers and a function name name to return the corresponding result.