Question

In: Computer Science

Python program problem: A group of statisticians at a local college has asked you to create...

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

Solutions

Expert Solution

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()


Related Solutions

PYTHON IS3073 1. A group of statisticians at a local college has asked you to create...
PYTHON IS3073 1. 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, as defined in Section 5.4. Define these functions 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...
Problem Description A local veterinarian at The Pet Boutique has asked you to create a program...
Problem Description A local veterinarian at The Pet Boutique has asked you to create a program for her office to create invoices for her patient’s office visits. When a customer brings their pet to the boutique, the clerk gets the Customer’s name, address, phone number and email address, as well as the pets name, pet type, pet age, and pet weight. After the customer sees the Veterinarian, the clerk determines the charges for the services, and medications for the office...
Python Problem Problem 1: In this problem you are asked to write a Python program to...
Python Problem Problem 1: In this problem you are asked to write a Python program to find the greatest and smallest elements in the list. The user gives the size of the list and its elements (positive and negative integers) as the input. Sample Output: Enter size of the list: 7 Enter element: 2 Enter element: 3 Enter element: 4 Enter element: 6 Enter element: 8 Enter element: 10 Enter element: 12 Greatest element in the list is: 12 Smallest...
You have been asked to create a python program that will ask the user how many...
You have been asked to create a python program that will ask the user how many tickets the user would like to purchase for graduation. The user will then choose the number of tickets needed and the program will list the cost of the tickets, a service fee, tax, and the total of the purchase. Specifications: The program should only accept whole numbers when receiving input for the number of tickets needed. No calculations should be performed if input is...
For Python: In this assignment you are asked to write a Python program to determine the...
For Python: In this assignment you are asked to write a Python program to determine the Academic Standing of a studentbased on their CGPA. The program should do the following: Prompt the user to enter his name. Prompt the user to enter his major. Prompt the user to enter grades for 3 subjects (A, B, C, D, F). Calculate the CGPA of the student. To calculate CGPA use the formula: CGPA = (quality points * credit hours) / credit hours...
Python Language: The Marietta Country Club has asked you to write a program to gather, then...
Python Language: The Marietta Country Club has asked you to write a program to gather, then display the results of the golf tournament played at the end of March. The Club president Mr. Martin has asked you to write two programs. The first program will input each player's first name, last name, handicap and golf score and then save these records in a file named golf.txt (each record will have a field for the first name, last name, handicap and...
The local driver's license office has asked you to design a program that grades the written...
The local driver's license office has asked you to design a program that grades the written portion of the driver's license exam. The exam has 20 multiple choice questions. Here are the correct answers: B D A A C A B A C D B C D A D C C B D A Your program should store these correct answers in an list. (Store each question's correct answer in an element of a String list). The program should ask...
The local driver's license office has asked you to design a program that grades the written...
The local driver's license office has asked you to design a program that grades the written portion of the driver's license test. The test has 20 multiple choice questions. Here are the correct answers: B D A A C A B A C D B C D A D C C B D A Your program should store these correct answers in an list. (Store each question's correct answer in an element of a String list). The program should ask...
Driver’s License Exam 20PTS PYTHON AND FLOWCHART The local driver’s license office has asked you to...
Driver’s License Exam 20PTS PYTHON AND FLOWCHART The local driver’s license office has asked you to design a program that grades the written portion of the driver’s license exam. The exam has 20 multiple choice questions. Here are the correct answers: 1.B 6.A 11.B 16.C 2.D 7.B 12.C 17.C 3.A 8.A 13.D 18.B 4.A 9.C 14.A 19.D 5.C 10.D 15.D 20.A Your program should store these correct answers in an array. (Store each question’s correct answer in an element of...
If the president of your college called you in and asked how a community college  could create...
If the president of your college called you in and asked how a community college  could create a competitive advantage for itself, what would you recommend? (Be sure to consider the costs as well as the benefits of your proposal and the economic aspect.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT