In: Computer Science
Challenge: Number Stats 2
Description: Extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file.
Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information including the determination of median and mode. It also provides experience adding new functionality to an existing program.
Requirements:
Extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file.
You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following:
The output from the program is to display the information described above using the following strings preceding the values. There is to be a space between the colon and the value.
File name:
Sum:
Count:
Average:
Maximum:
Minimum:
Range:
Median:
Mode:
At the end of one attempt at reading, or a successful read, of a file the user it to be asked if they would like to evaluate another file of numbers. Use the prompt: Would you like to evaluate another file? (y/n) If the user answers y, then the program is to accept input for another file name. If the user answers with anything other than y, the program is to exit.
The program you write must be able to handle four different circumstances for the file that contains the number data:
EDIT:
file 1:
728
44
591
586
660
852
554
419
451
698
947
660
170
396
627
975
748
13
373
881
112
266
500
476
951
433
409
786
592
251
324
930
148
785
101
929
583
505
487
175
995
875
913
620
444
806
259
8
893
637
635
238
140
989
476
438
835
641
650
904
268
798
464
716
918
377
332
691
981
370
747
217
668
777
475
322
362
540
209
300
884
245
375
400
915
331
765
522
222
849
907
461
507
976
575
671
755
901
263
542
file 2:
728
44
591
586
660
852
554
419
451
698
947
660
170
396
627
975
748
13
373
881
112
266
500
476
951
433
409
786
592
251
324
930
148
785
101
929
583
505
487
175
995
875
913
620
444
806
259
8
893
637
635
238
140
989
476
438
835
641
650
904
268
798
464
716
918
377
332
691
981
370
747
217
668
777
475
322
362
540
209
300
884
245
375
400
915
331
765
522
222
849
907
461
507
976
575
671
755
901
263
file 3:
728
file 4 is completely blank.
For the provided numbers.txt file the following are the values your program should generate:
Sum: 56110
Count: 100
Average: 561.1
Maximum: 995
Minimum: 8
Range: 987
Median: 564.5
Mode: [660, 476]
For the provided numbers2.txt file the following are the values your program should generate:
Sum: 55568
Count: 99
Average: 561.2929292929293
Maximum: 995
Minimum: 8
Range: 987
Median: 575
Mode: [660, 476]
For the provided numbers3.txt file the following are the values your program should generate:
Sum: 728
Count: 1
Average: 728.0
Maximum: 728
Minimum: 728
Range: 0
Median: 728
Mode: [728]
For the provided numbers4.txt file the program should generate:
There are no numbers in numbers4.txt
THANK YOU!! (:
Below is the Python code -
from collections import Counter
# There are unknown number of file(s) we're reading one
# another, that's the reason behind below while loop.
while True:
# This will prompt to enter the file name which we wish to read for stats.
# Remember to enter the file name with extension.
file_name = input('Enter the file name -')
# Opening the file in read(r) mode
file = open(file_name, 'r')
# Reading all the numbers from file.
# Since we're using readlines() func, data variable is the list of strings.
data = file.readlines()
# Converting all the strings into integers.
data = list(map(int, data))
# If file is empty then data is a empty list
if not data:
print('There are no numbers in',file_name)
# If we wish to read another file then input 'y' and then below continue will run
# else below break will run, and we're out of the program
if input('Would you like to evaluate another file? (y/n)') != 'y':
break
continue
# Sorting the numbers to find median easily
data.sort()
# Getting sum of all the numbers
sum_ = sum(data)
# Getting the count of numbers
count_ = len(data)
# Getting the average of all the numbers
average = sum_/len(data)
# Since we've sorted all the numbers in increasing order,
# the minimum and maximum element
# are the first and last element respectively.
maximum = data[-1]
minimum = data[0]
# Getting range of the numbers
range_ = maximum - minimum
# Below formula for median will get median for both even and odd numbers of data.
# For odd numbers of data we're just assigning the average between same
# number(that is middle element)
# But for even numbers of data these are two different middle elements.
median = (data[len(data)//2] + data[(len(data)-1)//2]) / 2
# Getting all the numbers in mode whose frequency is equal to the highest frequency.
mode = []
freq = Counter(data)
# mx will keep the current maximum frequency
mx = 0
for num in freq:
if freq[num] > mx:
mx = freq[num]
mode = []
if freq[num] == mx:
mode.append(num)
# Sorting the mode in decreasing order
mode.sort(reverse = True)
# Printing all in the given format -
print('File name:', file_name)
print('Sum:', sum_)
print('Count:', count_)
print('Average:', average)
print('Maximum:', maximum)
print('Minimum:', minimum)
print('Range:', range_)
print('Median:', median)
print('Mode:', mode)
# If we wish to read another file then input 'y' and then again while loop will run
# else below break will run, and we're out of the program
if input('Would you like to evaluate another file? (y/n)') != 'y':
break
Below is the interaction by running above code -
Enter the file name -numbers.txt
File name: numbers.txt
Sum: 56110
Count: 100
Average: 561.1
Maximum: 995
Minimum: 8
Range: 987
Median: 564.5
Mode: [660, 476]
Would you like to evaluate another file? (y/n)y
Enter the file name -numbers2.txt
File name: numbers2.txt
Sum: 55568
Count: 99
Average: 561.2929292929293
Maximum: 995
Minimum: 8
Range: 987
Median: 575.0
Mode: [660, 476]
Would you like to evaluate another file? (y/n)y
Enter the file name -numbers3.txt
File name: numbers3.txt
Sum: 728
Count: 1
Average: 728.0
Maximum: 728
Minimum: 728
Range: 0
Median: 728.0
Mode: [728]
Would you like to evaluate another file? (y/n)y
Enter the file name -numbers4.txt
There are no numbers in numbers4.txt
Would you like to evaluate another file? (y/n)n
Note - Its possible that the solution is not upto your expectation. So If at any point you think that some thing is wrong with the solution then comment down the clarification about it and then I will try to improve that part.
Hope it helps
Updated at 4:46 am UTC, Sunday, 25 October 2020