Informatics 42 • Winter 2012 • David G. Kay • UC Irvine

Second Homework

With this homework assignment, to be done individually (but with questions to i42@uci.edu or Piazza), we'll get into our usual rhythm of homeworks over the weekend and lab assignments in the lab hours).

Get your work checked and signed off by a classmate, then show it to your TA in lab by Monday, January 23.

Part I

Continue with the class-based restaurants program (RPList.py) that you worked on last week. You can start with the original version at http://www.ics.uci.edu/~kay/python/RPList.py or you can use the modified version (with sentences and erasing) you developed for the last homework (so long as that version works correctly).

Part II

This Python code copies a file, line by line. It presumes that the input and output files will be in the same directory (folder) as the code itself. (This is a restriction we could relax by using libraries that let us navigate around file systems and use the operating system's standard file dialog boxes. But those are topics for another day.)

inFileName = input("Please enter the name of the file to copy: ")
inFile = open(inFileName)
outFileName = input("Please enter the name of the new copy:  ")
outFile = open(outFileName, 'w')
for line in inFile:
  outFile.write(line)
inFile.close()
outFile.close()

(a) Install and run this code on your own system. Test it out with a short file.

Then download the Project Gutenberg version of The Adventures of Sherlock Holmes from http://www.gutenberg.org/cache/epub/1661/pg1661.txt (Project Gutenberg is a wonderful resource for non-copyright-protected texts). Run your file copying program to make a copy of this file. [Late update: Some problems have been reported with reading Project Gutenberg files. If you run into messages saying that Python can't decode a character, just find some other long text file and use that instead.]

(b) Modify a copy of your program so that the copied file includes line numbers at the start of each line:

    1: Project Gutenberg's The Adventures of Sherlock Holmes, by Arthur Conan Doyle
    2: 
    3: This eBook is for the use of anyone anywhere at no cost and with
...
13052: subscribe to our email newsletter to hear about new eBooks.
(Note that the line number is formatted and right-justified in a five-character field.)

Additional challenges: If you have some time and enjoy coding, try some of the following. If you don't have more time for more coding for this homework, that's okay; move on to the lab assignment and wait for next week's homework. (These additional challenges don't yield any actual extra credit, but if you implement some of them, we'll be impressed. It's generally good to impress instructors because it gives them something special to write in recommendation letters.) But it wouldn't hurt for everyone to read these and think about how to code them up, even if you don't actually do it.


Written by David G. Kay, Winter 2005. Modified Winter 2006. Modified by Alex Thornton, Winter 2007, and by David G. Kay, Winter 2008. Modified for Python by David G. Kay, Winter 2012.