Ask Question
23 March, 22:21

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 if the list is empty. Include a main function that tests the three statistical functions with a given list.

+2
Answers (1)
  1. 24 March, 01:45
    0
    see explaination

    Explanation:

    #Function receive list as a parameter & returns single value

    def median (list):

    #Checking for empty list

    if len (list) = = 0:

    return 0

    list. sort () #Arranging list of elements

    midIndex = int (len (list) / 2) #Determining middle element

    if len (list) % 2 = = 1:

    #return median when number of elements in list is odd

    return list[midIndex]

    else:

    #return median when number of elements in list is even

    return (list[midIndex] + list[midIndex - 1]) / 2

    #This function will calculate the mean of given list of numbers

    #Function receive list as a parameter & returns single value

    def mean (list):

    #Checking for empty list

    if len (list) = = 0:

    return 0

    total = 0

    for number in list:

    total + = number

    return total / len (list) #returns calculated mean

    #This function will calculate the mode of given list of numbers

    #Function receive list as a parameter & returns single value

    def mode (list):

    if len (list) = = 0:

    return 0

    numberDictionary = {} #Creating dictionary

    for digit in list:

    number = numberDictionary. get (digit, None)

    if number = = None:

    numberDictionary[digit] = 1

    else:

    numberDictionary[digit] = number + 1

    #Getting the maximum value from list

    maxValue = max (numberDictionary. values ())

    modeList = []

    for key in numberDictionary:

    #Determing elements which has the maximum value

    if numberDictionary[key] = = maxValue:

    modeList. append (key) #adding elements into list whose value is maximum

    return modeList #returns mode list

    def main ():

    #Creating list

    list = [3, 1, 7, 1, 4, 4, 10]

    print ("List:", list) #Printing list of elements

    #Printing mode of given list by calling function & passed list as a parameter

    print ("Mode: ", mode (list))

    #Printing median of given list by calling function & passed list as a parameter

    print ("Median : ", median (list))

    #Printing mean of given list by calling function & passed list as a parameter

    print ("Mean: ", mean (list))

    main ()
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “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 ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers