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

#File: numstat2.py # readFile function implementation def readFile(infile): arr

ID: 3858208 • Letter: #

Question

#File: numstat2.py

# readFile function implementation
def readFile(infile):
arr = []
for line in infile:
num = int(line)
arr.append(num)

return arr

# getSum function implementation
def getSum(arr):
  
total = 0
  
for n in arr:
total = total + n
  
return total

# getMaximum function implementation
def getMaximum(arr):

maximum = arr[0]

for n in arr:
if n > maximum:
maximum = n

return maximum

# getMinimum function implementation
def getMinimum(arr):

minimum = arr[0]

for n in arr:
if n < minimum:
minimum = n

return minimum

# getMedian function implementation

def getMedian(arr):
n = len(arr)
for i in range(n-1):
minPos = i

for j in range(i + 1, n-1):
if arr[j] < arr[minPos]:
minPos = j

if i != minPos:
temp = arr[i]
arr[i] = arr[minPos]
arr[minPos] = temp
if n < 1:
return None

if n % 2 == 1:
return arr[n/2]

else:
return sum(arr[n/2-1:n/2+1])/2.0

# getMode function implementation
def getMode(arr):
_mode = []
numCounts = {}
max_count = 0
for number in arr:
if number not in numCounts:
numCounts[number] = 0
numCounts[number] += 1
if numCounts[number] > max_count:
max_count = numCounts[number]
for number, count in numCounts.items():
if count == max_count:
_mode.append(number)

return _mode


# main function implementation
def main():
  
choice = "y"

while choice == 'y' or choice == 'Y':
filename = input('Enter the name of the file: ')
  
try:
infile = open(filename, 'r')

arr = readFile(infile)
total = getSum(arr)
count = len(arr)
if count > 0:
average = total / count
else:
average = 0
maximum = getMaximum(arr)
minimum = getMinimum(arr)
range1 = maximum - minimum
median = getMedian(arr)
mode = getMode(arr)

print(' File name: ',filename)
print('Sum: ',total)
print('Count: ',count)
print('Average: ',average)
print('Maximum: ',maximum)
print('Minimum: ', minimum)
print('Range: ',range1)
print('Median: ',median)
print('Mode: ',mode)

infile.close()
  
except:
print('The input file is not found!')

choice = input(' Would you like to evaluate another file? (y/n): ')

# call the main function
main()

My code will not read numbers from a file. It keeps saying my block statement no imput found.

I think I am having trouble with the median and mode because my program worked fine before I added those?

What I am trying to do is read a list of numbers from a file

like numbers.txt

1 10 100 14 15 18 36 83 and then give the max, min , mode, medain ex.....

Explanation / Answer

Bug found in getMedian method return statement. It should be - return sum(arr[n//2-1:n//2+1])/2.0#bug found

def readFile(infile):
  
arr = []
for line in infile:
for val in line.split():
arr.append(int(val))
return arr
# getSum function implementation
def getSum(arr):
  
total = 0
  
for n in arr:
total = total + n
  
return total
# getMaximum function implementation
def getMaximum(arr):
maximum = arr[0]
for n in arr:
if n > maximum:
maximum = n
return maximum
# getMinimum function implementation
def getMinimum(arr):
minimum = arr[0]
for n in arr:
if n < minimum:
minimum = n
return minimum
# getMedian function implementation
def getMedian(arr):
n = len(arr)
for i in range(n-1):
minPos = i
  
for j in range(i + 1, n-1):
if arr[j] < arr[minPos]:
minPos = j
  
if i != minPos:
temp = arr[i]
arr[i] = arr[minPos]
arr[minPos] = temp
if n < 1:
return None
if n % 2 == 1:
return arr[n/2]
else:
return sum(arr[n//2-1:n//2+1])/2.0#bug found
# getMode function implementation
def getMode(arr):
_mode = []
numCounts = {}
max_count = 0
for number in arr:
if number not in numCounts:
numCounts[number] = 0
numCounts[number] += 1
if numCounts[number] > max_count:
max_count = numCounts[number]
for number, count in numCounts.items():
if count == max_count:
_mode.append(number)
return _mode

# main function implementation
def main():
  
choice = "y"
while choice == 'y' or choice == 'Y':
filename = input('Enter the name of the file: ')
  
try:
infile = open(filename, 'r')
#print(infile.read())
arr = readFile(infile)
#print(arr)
total = getSum(arr)
count = len(arr)
if count > 0:
average = total / count
else:
average = 0
maximum = getMaximum(arr)
minimum = getMinimum(arr)
range1 = maximum - minimum
median = getMedian(arr)
mode = getMode(arr)
print(' File name: ',filename)
print('Sum: ',total)
print('Count: ',count)
print('Average: ',average)
print('Maximum: ',maximum)
print('Minimum: ', minimum)
print('Range: ',range1)
print('Median: ',median)
print('Mode: ',mode)
infile.close()
  
except:
print('The input file is not found!')
choice = input(' Would you like to evaluate another file? (y/n): ')
# call the main function
main()