Please use Python Script, THANK YOU The script must define 4 functions read_file
ID: 3693674 • Letter: P
Question
Please use Python Script, THANK YOU
The script must define 4 functions
read_file_into_integer_list
list_mean
list_median
list_range
read_file_into_integer_list
This functions must have the following header
This function must read in the numbers contained in a file, convert them to integers and add them to an array.
The function must return the array of integers it creates.
list_mean
This functions must have the following header
This function must calculate the average (mean) of the integers in a list.
It must return the the rounded average.
list_median
This functions must have the following header
This function must sort a list and return the value of the element in the middle of the list
list_range
This functions must have the following header
This function must return the difference between the highest and lowest number in a list.
Run code
At the bottom of the script you must have the following code
Testing
Your output should look like this
Explanation / Answer
def read_file_into_integer_list(name):
L = []
f = open(name)
for line in f.readlines():
line = line.split()
for a in line:
L.append(int(a))
return L
def list_mean(numbers):
sum = 0
count=0
for i in numbers:
sum += i
count+=1
return (sum*1.0)/count
def list_median(numbers):
numbers.sort()
l = len(numbers)
return numbers[int(l/2)]
def list_range(numbers):
numbers.sort()
l = len(numbers)
return numbers[l-1]-numbers[0]
numbers = read_file_into_integer_list("numbers.txt")
print("numbers: ", numbers)
print("list_mean(numbers): ", list_mean(numbers))
numbers.sort()
print("numbers: ", numbers)
print("list_median(numbers):", list_median(numbers))
print("list_range(numbers): ", list_range(numbers))