Question
# readFile function implementation def readFile(infile): data = [] for line in infile: num = int(line) data.append(num) return data # getSum function implementation def getSum(data):
# readFile function implementation
def readFile(infile):
data = []
for line in infile:
num = int(line)
data.append(num)
return data
# getSum function implementation
def getSum(data):
total = 0
for n in data:
total = total + n
return total
# getMaximum function implementation
def getMaximum(data):
maximum = data[0]
for n in data:
if n > maximum:
maximum = n
return maximum
# getMinimum function implementation
def getMinimum(data):
minimum = data[0]
for n in data:
if n < minimum:
minimum = n
return minimum
# getMedian function implementation
def getMedian(data):
n = len(data)
for i in range(n-1):
minPos = i
for j in range(i + 1, n-1):
if data[j] < data[minPos]:
minPos = j
if i != minPos:
temp = data[i]
data[i] = data[minPos]
data[minPos] = temp
if n > 0 and n % 2 == 0:
median = (data[int(n/2)] + data[int(n/2) - 1]) / 2.0
elif n > 0:
median = data[int(n/2)]
else:
median = 0
return median
# getMode function implementation
def getMode(data):
sort(ar)
length = getCount(data)
maxim = 1
mode = []
if length is 1 or 0:
if length is 1:
mode.append(str(ar[0]))
return mode
else:
return mode
i = 0
count = 1
while i < length-1:
if ar[i] != ar[i+1]:
count=1;
else:
count = count + 1
if count > maxim:
maxim = count
i = i + 1
if ar[i] == ar[i-1]:
count = count + 1
if count > maxim:
maxim = count
i = 0
count = 1
while i < length-1 :
if ar[i] != ar[i+1]:
count=1;
else:
count = count + 1
if count == maxim:
mode.append(str(ar[i]))
i = i + 1
if ar[i] == ar[i-1]:
count = count + 1
if count == maxim:
mode.append(str(ar[i]))
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')
data = readFile(infile)
total = getSum(data)
count = len(data)
if count > 0:
average = total / count
else:
average = 0
maximum = getMaximum(data)
minimum = getMinimum(data)
range1 = maximum - minimum
median = getMedian(data)
mode = getMode(data)
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()
Keeps using my block statement? Having trouble with mode again. needs two modes for even and odd. Thanks for your help
like a list of numbers num.py
and it should give median, range, max, min
mode should be two options [100, 50] for example, may need to use dictionary {} python 3.6
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started