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 the below sample programs: · mode.py · median.py 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.
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 the below sample programs:
· mode.py
· median.py
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.
***mode.py code***
"""
File: mode.py
Prints the mode of a set of numbers in a file.
"""
fileName = input("Enter the file name: ")
f = open(fileName, 'r')
# Input the text, convert its to words to uppercase, and
# add the words to a list
words = []
for line in f:
wordsInLine = line.split()
for word in wordsInLine:
words.append(word.upper())
# Obtain the set of unique words and their
# frequencies, saving these associations in
# a dictionary
theDictionary = {}
for word in words:
number = theDictionary.get(word, None)
if number == None:
# word entered for the first time
theDictionary[word] = 1
else:
# word already seen, increment its number
theDictionary[word] = number + 1
# Find the mode by obtaining the maximum value
# in the dictionary and determining its key
theMaximum = max(theDictionary.values())
for key in theDictionary:
if theDictionary[key] == theMaximum:
print("The mode is", key)
break
***median.py code***
"""
File: median.py
Prints the median of a set of numbers in a file.
"""
fileName = input("Enter the file name: ")
f = open(fileName, 'r')
# Input the text, convert it to numbers, and
# add the numbers to a list
numbers = []
for line in f:
words = line.split()
for word in words:
numbers.append(float(word))
# Sort the list and print the number at its midpoint
numbers.sort()
midpoint = len(numbers) // 2
print("The median is", end=" ")
if len(numbers) % 2 == 1:
print(numbers[midpoint])
else:
print((numbers[midpoint] + numbers[midpoint - 1]) / 2)
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images