CS313E Assignment 1: More Python Programming (10 points)

Due: by Monday, September 17, 2012.

Your program listing should have the following information.

#  Files: Assignment1.py
#  Description:
#  Student's Name:
#  Student's UT EID:
#  Course Name: CS 313E 
#  Date Created:
#  Date Last Modified:

The Assignment

One of the things we're going to be doing a lot of this semester is processing data from a file. Assume you have a file containing words, one per line. Here's such a file: word list. Save that word list to your directory area. Write a Python program to read successive words from that list, see if the word meets some test, and write to another file exactly those words that pass the test. The test should be in the form of a Boolean valued function. For example, if you want to output exactly the words of length 5, you might write the "filter" function:
def hasLength5( word ):
    return len( word ) == 5
Write your chief function with the template below. Formal parameters inputFileName and outputFileName are strings that are the names of the files you are reading from and writing to, respectively. The input file has to exist, but the output file is created by the program. Finally, f is a functional parameter. You pass in the name of the filter function you're applying. Your program should also count how many words you read from input, and how many you wrote to output, and print that information in a nice format.

def filterFile( inputFileName, outputFileName, f ):
    # create a file object associated with the input
    # file specified and open for reading
    myinputFile  = open(inputFileName, 'r')

    # create a file object associated with the output
    # file specified and open for writing
    myoutputFile = open(outputFileName, 'w')


    # This assumes that there is only one word per line and
    # no extra stuff on the line, like blank space. 
    for word in inputFile:
        if f( word ):
           # write the word to the output file
           myoutputFile.write( word )

    # print a summary of how many words you output 
    # that satisfy f.

For example, if your input file is named myWordList and you want to produce a file called wordsOfLength5 using the hasLength5 function defined above, you'd use the following call:
filterFile( "myWordList", "wordsOfLength5", hasLength5 )
You shouldn't have to change your main function at all for the different calls.

Write filter functions and calls to do the following:

  1. Get all words of length 7 from the list. Name the output file wordsOfLength7.
  2. Get all words from the list containing the letter "q". Name the output file wordsQ.
  3. Get all words from the list that start with the letter "l". Name the output file LWords.
  4. Get all words from the list that are palindromes (read the same backwards and forwards. Name the output file palindromes.
Your filter functions, filterFile function, and calls should all be in one file. Your program should run once and generate four output files.