File Reading/Writing Exercises #1

Example:

Suppose you were given the data file Fred.txt below, with one floating-point number per line (but with possible blank lines), and were asked to create the function Mean(input_filename, output_filename) that calculated the mean of all the numbers in the file whose name is in the argument input_filename and to put its answer into the file output_filename in the form:

The mean of the numbers in Fred.txt is 6.5
 

Fred.txt

-5
13.5

11

Here is the code which will do that...

def Mean(input_filename, output_filename):
    '''Calculates and outputs the mean of the numbers in the input_filename'''
    # read the file's contents, and separate into words...
    get_it = open(input_filename, 'r')
    s = get_it.read()
    words = s.split()  # we now have an array of "words"
    get_it.close()

    n = 0  # counting the numbers
    the_sum = 0.0  # adding the numbers
    for a_word in words:
        if a_word != '':
            the_sum += float(a_word)  # REMEMBER: convert the string into a number 
            n += 1

    the_mean = the_sum / n

    # write the answer to the output file
    put_it = open(output_filename, 'w')
    s = 'The mean of the numbers in '+input_filename+' is '+str(the_mean)
    put_it.write(s)
    put_it.close()

... when called this way:  Mean('Fred.txt','Fred-answer.txt')


Exercises:

1. Create the function ReverseLines(input_filename, output_filename) which will reverse the string of each line of the input-file and write it to the output file.  Here's the result of calling ReverseLines('BeforeReverse.txt', 'AfterReverse.txt'):

BeforeReverse.txt

Hi there,
ye
all.
 

AfterReverse.txt

,ereht iH
ey
.lla

 

2. Create the function Median(input_filename, output_filename) which will read the same type of files as Mean() in the example code above, and calculate the median of the numbers, and write the answer in the same way except replacing the word "mean" with "median" in the output file.  As a test, run it in on a file that has just the numbers:   6, 4, 5 and 3 (on separate lines), and make sure that you get 4.5 as the median in the output file.  Also, run it on a file containing just 4, 6 and 5 and make sure you get 5.

 

3. Imagine that you are correcting some arithmetic exercises that elementary school kids are doing.  They have been given a worksheet with a whole set of tasks like: "12 * 3 = ?" and "345 - 127 = ?" and have typed all the answers into a file that looks like this:

12 * 3 = 36
345 - 127 = 32
421 + 13 = 433
16 * 25 = 400

Notice that the second and third answers are wrong.  Write a function called CorrectingHomework(in_file, out_file) that reads all the lines that a student has written in in_file and writes to out_file every mistaken line, in the following form, and tallies up the number of correct answers divided by the total number of answers and provides the final grade as a percentage.

345 - 127 = 218 (not 32)
421 + 13 = 434 (not 433)

Correct: 2/4  Grade: 50%

Therefore, your function must perform the arithmetic and check whether the student's answer is correct, and if not, write the appropriate line in the out_file for each mistaken line.

Here's what you should know about the lines in the input file:

 

4. Challenge problem: we are going to look at average SAT scores for the students at the high schools in NYC for 2012.