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).

• Add the menu item "```c: Change all prices```". This should prompt the user for an amount representing a percentage change in price (so that 100 would double a price and –50 would cut it in half). Then it should apply that price change to the prices for all the restaurants in the collection.

This will require a new method in the Restaurant class and a new method in the Collection class. (Try to work this out for yourself; you've seen all the components and patterns. Only read the following details if you're stuck [really, now, stop reading and think before continuing this paragraph]: The method in the Restaurant class, let's call it `adjustPrice`, takes the percentage number and changes the Restaurant's price (`self.price`) accordingly. The method in the Collection class applies the Restaurant's `adjustPrice` method to each Restaurant in the Collection (which you can do with a loop (`for r in self.rests:` ...) or with a list comprehension.)

• Add an `averagePrice` method to the Collection class; it should return a `float` representing the average price at all the restaurants. You know the formula for calculating an average and you know how to obtain the constituent partsâ€”the total price [following code we've seen in class] and the number of restaurants in the Collection. To be thorough, this method should check that the collection isn't empty; if it is empty, it should print a message and return zero. Then modify Collection's `print` method to print the average price after it prints all the restaurants.

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
...
(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.

• Write a program called Reverse. It should prompt for two file names and copy the first file to the second, except in reverse order (with the last line of the original file as the first line of the new one). This requires that you store the whole file in your program; probably the best way is with the `f.readlines()` method..

• Implement a simple version of the diff command that reads two files and prints out the lines that don't match. (The real diff will recognize where one file has an extra line, and not act as if nothing matches after that point, but your program doesn't have to do this.)

• Implement head and tail, with the number of lines and the file name as arguments. Doing head is easy; doing tail is a little trickier.

• If you examine the file from Project Gutenberg, you see that it contains some "housekeeping" information at the beginning and at the end. You'll also see that the text itself starts after a line beginning with "*** START" and ends just before a line beginning with "*** END". Write the program CopyTextOnly that copies only the body of a Project Gutenberg file, omitting the "housekeeping" material at the front and end.

• Write a program Mirror that's like Reverse above, except that it also reverses each line.

• Modify the line-numbering program so that the line number field is no wider than it has to be. (So, for example, a file with under 1000 lines, but over 99, would have its line numbers in a three-character-wide field.) You'll need to read the file into memory (as Reverse did) or read it twice (which we generally wouldn't do if the file is short enough to fit in memory, because I/O is so much slower than processing data already in memory).

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.