It must be in an hw11 directory, which you must create inside a hw directory ins
ID: 3571851 • Letter: I
Question
It must be in an hw11 directory, which you must create inside a hw directory inside you it116 directory.
Make sure the script obeys all the rules in the Script Requirements page.
Copy the file numbs.txt, which you will find in /home/ghoffman/course_files/it116_files into your hw11 directory .
Specification
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
Be sure to run this script on the Unix machine so you know it works in the environment in which I will run it when I score your homework.
numbs.txt 46 19 35 43 35 33 42 6 25 6 19 31 1 23 14 18 24 24
Python Please.
Explanation / Answer
def read_file_into_integer_list( filename ):
#assumes there is one word in each line
file = open( filename, 'r' );
listIs = [];
for line in file:
try:
listIs.append( int(line) );
except:
pass;
return listIs;
def list_mean( listIs ):
mean = 0;
for i in range(len(listIs)):
mean = mean + listIs[i];
mean = (mean*1.0)/len(listIs);
return mean;
def list_median( listIs ):
copyOfList =[];
for i in listIs:
copyOfList.append( i );
copyOfList.sort();
length = len(listIs);
if( length%2 == 0 ):
median = ( copyOfList[ int(length)/2 ] + copyOfList[ int(length)/2 - 1 ] )/2.0;
else:
median = copyOfList[ int(length)/2 ];
return median;
def list_range( listIs ):
maxIs = listIs[0];
minIs = listIs[0];
for x in listIs:
maxIs = max( maxIs , x );
minIs = min( minIs , x );
return maxIs-minIs;
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));
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.