Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this programming assignment you are to extend the program you wrote for Numbe

ID: 3857743 • Letter: I

Question

In this programming assignment you are to 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 name of the file.

The sum of the numbers.

The count of how many numbers are in the file.

The average of the numbers. The average is the sum of the numbers divided by how many there are.

The maximum value.

The minimum value.

The range of the values. The range is the maximum value minus the minimum value.

The median of the numbers.

The 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.

Testing

Once you have written your program you need to test it. Four test files containing data are supplied in a file attached to the assignment called numbers.zip. You must unzip the file to get the files numbers.txt, numbers2.txt, numbers3.txt and numbers4.txt.

Notes about the provided data files:

numbers.txt - contains the original test list of numbers from the Number Stats assignment. This file contains an even count of numbers.

numbers2.txt - has one number removed from numbers.txt to make the list contain an odd count of numbers.

numbers3.txt - has one number in it to test this "edge" case to make sure the program works when only one numbers is provided.

numbers4.txt - contains no numbers to test this "edge" case to make sure it doesn't crash the program and is reported to user.

The numbers.txt file, or any other test file containing numbers, needs to be in the same directory as the python program for it to be found.

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

---Use any numbers you would like the abover text files are not loading

Explanation / Answer

#!usr/bin/python

filename = raw_input("Enter filename ")
list = []
with open(filename,'r') as f:
    for line in f:
        for word in line.split():
            list.append(int(word))

for i in range(len(list)):
    j = i
    while j < len(list):
       if list[i] > list[j]:
          temp = list[i]
          list[i] = list[j]
          list[j] = temp
       j = j+1
max = list[len(list)-1]
min = list[0]

sum = 0
for i in range(len(list)):
    sum = sum + list[i]
avg = sum/len(list)
range1 = list[len(list)-1] - list[0]
if len(list) % 2 == 0:
    median = (list[len(list)/2] + list[(len(list)/2) + 1])/2
else:
    median = list[ (len(list)+1)/2]

list2 = []
for i in range(len(list)):
    j = i+1
   
    count = 1
    while j < len(list):
       if list[i] == list[j]:
          count = count + 1
       j = j+1
    list2.append(count)
   

mode = []

value = 1
index = 0
for i in range(len(list2)):
    if value < list2[i]:
       value = list2[i]
       index = i

if value > 1:
   for i in range(len(list2)):
       if value == list2[i]:
          mode.append(list[i])

print "File name: " + filename
print "Sum: " , sum
print "Count: " , len(list)
print "Average: " , avg
print "Maximum: " , max
print "Minimum: " , min
print "Range: " , range1
print "Median: " , median
if len(mode) > 0:
   print "Mode: " , mode
else:
   print "Mode: " + "No Mode exist"

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote