Question: In Python - Fundamentals of Python First Programs (2nd Edition) A group of statisticians at a local college has asked you to create a set

In Python - Fundamentals of Python First Programs (2nd Edition)

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

"""

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)

"""

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!