Question

In: Computer Science

Challenge: Number Stats 2 Description: Extend the program you wrote for Number Stats to determine the...

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:

  • Name of the file.
  • Sum of the numbers.
  • Count of how many numbers are in the file.
  • Average of the numbers. The average is the sum of the numbers divided by how many there are.
  • Maximum value.
  • Minimum value.
  • Range of the values. The range is the maximum value minus the minimum value.
  • Median of the numbers.
  • Mode of the numbers.

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:

  • The count of the numbers in the file being even.
  • The count of the numbers in the file being odd.
  • The file containing only one number.
  • The file containing no numbers. (An empty file.) The user is to be told that no numbers could be found in the file.
  • The file containing an even or odd count of numbers is an issue when calculating the median which is discussed below in the Useful Information section.

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!! (:

Solutions

Expert Solution

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


Related Solutions

Split the Number IN JAVASCRIPT Programming challenge description: You are given a number N and a...
Split the Number IN JAVASCRIPT Programming challenge description: You are given a number N and a pattern. The pattern consists of lowercase latin letters and one operation "+" or "-". The challenge is to split the number and evaluate it according to this pattern e.g. 1232 ab+cd -> a:1, b:2, c:3, d:2 -> 12+32 -> 44 Input: Your program should read lines from standard input. Each line contains the number and the pattern separated by a single whitespace. The number...
Description: In this program, you'll reuse the Monster data type you wrote in LA11A. Then, you'll...
Description: In this program, you'll reuse the Monster data type you wrote in LA11A. Then, you'll write a function that accepts a Monster as an argument by reference, then print the Monster to the screen. Again, this will be structured as a guided multiple choice quiz where you will choose the appropriate code for a program. After you have gotten a perfect score, write out the program and compile and run it to see it in action. Instructions: Choose the...
Short description of your assigned challenge Describe the innovation you researched to address the challenge The...
Short description of your assigned challenge Describe the innovation you researched to address the challenge The result (what did it prove?) How/Why it impacts or supports the challenge the challenge is "make Solar energy economical"
3.         A particular Intersession stats prof is noted for 2 things: the number of pages of...
3.         A particular Intersession stats prof is noted for 2 things: the number of pages of notes his students have to write down each night and the number of pieces of chalk he destroys each night. To see if there is any relationship between these, a record is kept of the number of pages of notes his students write during 6 different lectures in June and the number of pieces of chalk the prof destroys on these nights. The following...
For example, suppose you had a program to determine if a number is positive, negative or zero.
  For example, suppose you had a program to determine if a number is positive, negative or zero. Read in a number and then use the if statement to determine if the number is positive (>0) or negative (<0) or zero (==0): if (x>0)//display positive messageelse if (x<0)   //display negative messageelse//display zero message
Extend the program by adding rules for the following family relationships (add more facts as you...
Extend the program by adding rules for the following family relationships (add more facts as you need ). Rules father(X.Y) > Father (z,w) where X is Y's father father(X,Y):>male(X),Parent(X, Y). brother(X, Y): where X is Y's brother brother(X,Y):-male().praent(X,Y). Brother (y.w) sister(X,Y):-famale(X),praent(X, Y). sister(X, Y): where X is Y's sister Sister(w.y) son(X, Y)> son(X,Y):-male(X), praent(Y,X). where X is Y's son Son(y.z) daughter(X, Y) :- Daughter (w,x) where X is Y's daughter daughter(X,Y):-famale(X), praent(Y.X). where X and Y are sibling Sibling(X, Y):-praent(X,Y),...
1- Create a Java program to determine a certain number whether that number is odd or...
1- Create a Java program to determine a certain number whether that number is odd or even. Hint : use arithmetic operators and expressions to find the odd or even numbers. 2- Create a Java program to ask a user to enter a name and password as shown below: name is “Ahmed” and his password is 2321 or name is “Ali” and his password is 6776 . The program shows a greeting “Hi ..Welcome to my program” if the user...
Previously, you wrote a program named Admission for a college admissions office in which the user...
Previously, you wrote a program named Admission for a college admissions office in which the user enters a numeric high school grade point average and an admission test score. The program displays Accept or Reject based on those values. Now, create a modified program named AdmissionModularized in which the grade point average and test score are passed to a method that returns a string containing Accept or Reject. CODE: using System; using static System.Console; class Admission {    static void Main()...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement:...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1...
an investigator wants to determine if a new program aimed at reducing the overall number of...
an investigator wants to determine if a new program aimed at reducing the overall number of pregnancies in a highly populated area is effective (reducing average births below 6 per household). In order to carry out the investigation, the investigator surveys 20 participants in the program and finds the average number of births is 4.7 with a sample standard deviation of 0.9 births. carry out a test of hypothesis to test the investigator’s claim by answering the following with alpha=.01...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT