I have a separate code file for the mean, median, and mode; I just need a new python code which will open or print the mean, median, and mode on a single python code
I have a separate code file for the mean, median, and mode; I just need a new python code which will open or print the mean, median, and mode on a single python code
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
100%
I have a separate code file for the mean, median, and mode; I just need a new python code which will open or print the mean, median, and mode on a single python code. The data or numbers to compute the mean, median, and mode is in txt file named "numbers.txt"
Just create a separate python file which will read the .txt file (numbers.txt) and will open my code in mean.py, median.py, and mode.py; then print them out.
See my code for mean, median, and mode below; as well as the .txt file for your reference.
***numbers.txt***
45
66
22
10
15
88
15
31
90
***mean.py***
"""
File: mean.py
Prints the mean of a set of numbers in a file.
"""
fileName = input("Enter the file name: ") # Input fileName
f = open(fileName, 'r') # Open the file in read mode
sum=0 # Initialize sum to 0
count=0 # Initialize count to 0
for line in f.readlines(): # For each line in file
sum+=float(line.strip()) # Add the number to sum
count+=1 # Increment count by 1
print ('The Mean is:', (sum/count)) # Outputs the Mean (Average)
***median.py***
"""
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)
***mode.py***
"""
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
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps with 1 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education