CS8 Programming Assignment 4

CS8 Programming Assignment 4

Description

In this programming assignment, you will write programs that read from text files, and write into them. The basic python routines for opening files for reading and writing are:
mf1 = open("file1.txt","r")
mf2 = open("file2.txt","w")
The above commands assume that the current working directory Python uses contains the text file "file1.txt". The second file need not exist, and at the execution of the "open" command an empty file will be created. If the second file file2.txt exists prior to the command mf2 = open("file2.txt","w"), then its content will be destroyed (i.e., it will become an empty file).

The variables "mf1" and "mf2" are logical links to your text files within Python's execution environment. To read the first line from mf1, you may run

 s = mf1.readline()
This command reads the first line as a string and assigns to the string variable s. If you enter s to the Python prompt, you will see the line you read from the file followed by a newline character "\n", such as 'aardvark\n'. If you run the same command the second time
 t = mf1.readline()
Python reads the second line from the file mf1 and assigns it the string t. File reading and writing is a sequential operation. The next time "mf1.readline()" is executed the third line will be read, and so on.

A faster way to read from a file is in a "for" loop:

for aline in mf1:
    Statement1
    Statement2
assuming the Statement1 or Statement2 uses the string "aline" in some way.

Similarly a string can be written into the file mf2 ("file2.txt") which was created for the purpose of writing. You can write to a file in several different ways, such as

mf2.write("This is the first line\n")

s = "This is the second line\n"
mf2.write(s)

x = 123
t = str(x)
mf2.write(t)
In the last one, string "123" is written into the file (without the EOL character). If you wish to add an EOL character, you may use
x = 123
t = str(x)
t = t + "\n"
mf2.write(t)

After all reading and writing operations, the opened files need to be closed.

mf1.close()
mf2.close()
Closing a file is particularly necessary for files written into, otherwise they will remain empty files. For more information, refer to Python documentation for input and output: url

Requirements

I am providing a text file containing 58,112 words. You can download using the link: words.txt using the right-click "Save Link As". Place this file into the directory where you will have your pa4.py program. This way your code can easily find the text file with no extra work on your end. This is for your testing purposes and you do not need to submit words.txt. We already have a copy!
  1. Write a function named getListBegin(c,ifile,ofile) that takes the character c as the input and places all words in "ifile" that starts with the character c into another new file "ofile" in the same order. Run this program with different inputs, making sure that it works. For example, when we run it
    mf1 = open("words.txt","r")
    mf2 = open("blist.txt","w")
    getListBegin("d",mf1,mf2)
    mf1.close()
    mf2.close()
    
    we should see that the text file "blist.txt" contains all words in "words.txt" that starts with letter "d":
    dab
    dabbed
    dabbing
    dabble
    dabbled
    dabbler
    dabbles
    ..
    
    It is important that the output file not have any empty lines.

  2. Write a function named getListEnd(c,ifile,ofile) that takes the character c as the input and places all words in "ifile" that ends with the character c into another new file "ofile" in the same order. Run this program with different inputs, making sure that it works. For example, when we run it
    mf1 = open("words.txt","r")
    mf2 = open("elist.txt","w")
    getListEnd("f",mf1,mf2)
    mf1.close()
    mf2.close()
    
    we should see that the text file "elist.txt" contains all words in "words.txt" that ends with letter "f":
    aardwolf
    aloof
    aperitif
    bailiff
    basrelief
    beef
    behalf
    belief
    bluff
    ..
    
    It is important that the output file not have any empty lines.
    Remember that every line in "words.txt" ends with the EOL character.

  3. Write a function named getListContain(s,ifile,ofile) function that takes the string s (not just a single character) as the input and extracts all words that contain that string and writes them into a new file "ofile" in the same order. Run this program with different inputs, making sure that it works. For example, when we run it
    mf1 = open("words.txt","r")
    mf2 = open("clist.txt","w")
    getListContain("orda",mf1,mf2)
    mf1.close()
    mf2.close()
    
    we should see that the text file "clist.txt" contains all words that contain the string "orda":
    accordance
    affordability
    affordable
    chordal
    concordance
    ..
    
    It is important that the output file not have any empty lines.

  4. Write a function named getListRhyme(s,ifile,ofile) function that takes the string s (not just a single character) as the input and extracts all words that contain that string as its suffix, and writes them into a new file "ofile" in the same order. Run this program with different inputs, making sure that it works. For example, when we run it
    mf1 = open("words.txt","r")
    mf2 = open("rlist.txt","w")
    getListRhyme("gated",mf1,mf2)
    mf1.close()
    mf2.close()
    
    we should see that the text file "rlist.txt" contains all words that rhymes with the string "ted":
    abrogated
    aggregated
    castigated
    circumnavigated
    congregated
    conjugated
    corrugated
    delegated
    ...
    
    It is important that the output file not have any empty lines.
    Remember that every line in "words.txt" ends with the EOL character.

  5. Write a function named getListCount(n,ifile,ofile) function that takes the positive integer (1,2,3,..) and extracts all words that has exactly n letters, abd writes them into a new file "ofile" in the same order. Run this program with different inputs, making sure that it works. For example, when we run it
    mf1 = open("words.txt","r")
    mf2 = open("nlist.txt","w")
    getListCount(4,mf1,mf2)
    mf1.close()
    mf2.close()
    
    we should see that the text file "nlist.txt" contains all words that has exactly 4 letters:
    abbe
    abel
    abet
    able
    ably
    abut
    ..
    
    It is important that the output file not have any empty lines.

What to Submit

Functions to be submitted:
getListBegin(c,ifile,ofile)
getListEnd(c,ifile,ofile)
getListContain(s,ifile,ofile)
getListRhyme(s,ifile,ofile)
getListCount(n,ifile,ofile)

Write all 5 functions in a Python text file named pa4.py, and submit your file using the Dropbox link. Submit only the functions; no test programs outside the functions are needed. Also, you may write and include in your pa4.py file as many helper functions as you want. Furthermore, your functions do not print or return any value; they read from the input file ifile and write into output file ofile. While you are testing your functions, you can open files for reading and writing, however, these 5 functions do not open files. They receive links to the files that are already opened. For example, a typical test program (on my laptop) will look like below, assuming the file words.txt is located in my Desktop:

import os
os.chdir("/Users/koc/Desktop")

mf1 = open("words.txt","r")
mf2 = open("nlist.txt","w")
getListCount(4,mf1,mf2)
mf1.close()
mf2.close()
The function getListCount does not open or close the files. It only reads from mf1 and writes to mf2. The test program opens the file words.txt (which is located in my Desktop) and creates the logical link mf1 to this file, and lets the function getListCount to read from it by passing mf1 to the function. Similarly, the output file nlist.txt is created on my Desktop by the test program.

This programming assignment is graded out of 15 points, i.e., it has 5 points of extra credit.

Important: Add your full name and email address as a comment in the first line of your file, such as:
# Alice Smartpants (smartpants@umail.ucsb.edu)


cs8 web page