In: Computer Science
Python program problem:
A group of statisticians at a local college has asked you to create a set of functions that compute the median and mode of a set of numbers. Define these functions, median and mode, in a module named stats.py. Also include a function named mean, which computes the average of a set of numbers. Each function should expect a list of numbers as an argument and return a single number. Each function should return 0 if the list is empty. Include a main function that tests the three statistical functions using the following list defined in main: userList = [3, 1, 7, 1, 4, 10] An example of the program output is shown below: List: [3, 1, 7, 1, 4, 10] Mode: 1 Median: 3.5 Mean: 4.33
Program:
File name: "stats.py"



Output:

Editable code:
#Define the function "mean()"
def mean(l1):
#Check if the list is empty
if l1 == []:
#Return zero
return 0
#Otherwise, calculate the total using the function "sum()"
tot = sum(l1)
#Calculate the count using the function "len()"
count = len(l1)
#Calculate the mean value
res = tot / count
#Return the mean value
return res
#Define the function "median()"
def median(l1):
#Check if the list is empty
if l1 ==[]:
#Return zero
return 0
#Otherwise
else:
#Sort the list
l1.sort()
#Check the condition
if(len(l1)%2!=0):
#True, calculate and return the value
return l1[len(l1)//2]
#Otherwise
else:
#Calculate the value and assign it into the variable "t"
t=len(l1)//2
#Return the median value
return (l1[t]+l1[t-1])/2
#Define the function "mode()"
def mode(l1):
#Check if the list is empty
if l1 == []:
#Return zero
return 0
#Variable initialization
c = 0
n = l1[0]
#Loop to iterate the numbers in list "l1"
for num in l1:
#Check the condition
if l1.count(num) > c:
#Assign the value into the variable "c"
c = l1.count(num)
#Assign the "num" into "n"
n = num
#Return the mode value
return n
#Define the "main()" function
def main():
#Declare the list "l1"
l1 = [3, 1, 7, 1, 4, 10]
#Print the list
print('List: ', l1)
#Print the mode value
print('Mode : ', mode(l1))
#Print the median value
print('Median : ', median(l1))
#Print the mean value
print('Mean : ', mean(l1))
# call the function
if __name__ == '__main__':
main()