Python scrypting help! The script must define 4 functions read_file_into_integer
ID: 3572512 • Letter: P
Question
Python scrypting help!
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
# Python code begins
numbers = []
length=0
# Function definition is here
def read_file_into_integer_list(filename):
i=0
with open(filename) as f:
for line in f:
data = line.split()
for j in data:
numbers.append(int(j))
#print (numbers[i])
i=i+1;
length=i;
return numbers;
def list_mean(a2):
length=len(numbers)
mean =sum(a2)
return mean/length
def list_median(a3):
a3.sort();
length=len(a3)
middle=int(length/2)
return a3[middle]
def list_range(a4):
a4.sort();
length=len(a4)
#print(a4)
return a4[length-1]-a4[0]
# Now you can call printme function
numbers = read_file_into_integer_list("numbs.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))
Sample Output :
numbers: [1, 2, 3, 4, 5, 6]
list_mean(numbers): 3.5
numbers: [1, 2, 3, 4, 5, 6]
list_median(numbers): 4
list_range(numbers): 5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.