Informatics 42 • Winter 2012 • David G. Kay • UC Irvine
With this homework assignment, to be done individually (but with questions to firstname.lastname@example.org 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.
Continue with the class-based restaurants
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).
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.
adjustPrice, takes the percentage number and changes the Restaurant's price (
self.price) accordingly. The method in the Collection class applies the Restaurant's
adjustPricemethod to each Restaurant in the Collection (which you can do with a loop (
for r in self.rests:...) or with a list comprehension.)
averagePricemethod to the Collection class; it should return a
floatrepresenting 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
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
(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:
(Note that the line number is formatted and right-justified in a five-character field.)
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.
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.