Computer Science 111a - Fall Semester, 2006
Homework #7 (also serves as Lab7)
due: by midnight Tuesday October 24, 2006
Your task in this week's project is to use a file as a database. You'll
read in the contents, a list of book titles,
and allow the user to add more, sort the file
in lexicographic (alphabetical) order, then write
the whole thing back out to the same file. For now we will use a simple
although inefficient sorting algorithm, and will return to more
efficient sorts later in the semester.
Work one step at a time, getting each step to work
before moving on:
- type getcopy books.dat
- type getcopy hw7demo.pyc to run the demonstration
program. When you are finished running the program, look
at the file books.dat (emacs is the easiest way).
To obtain the original version of books.dat, just
do getcopy books.dat again to overwrite the old one.
- Now start writing your program:
In the definition of main(),
read in the strings from the file called "books.dat"
(these are the titles of the books,
and there is one title per line in the file). This
step is similar to the example hw4Files.py.
Use the readlines() function, and place the resulting
list of strings in a variable called bookList.
After you have read the titles, close the file.
- Write the definition of a function called printList() that has
one parameter, called bookList. It prints out all the strings in
bookList, one string per line. It does not return anything.
In your main() function definition, call printList(), passing
it the list of books you read in from the file. You
stored these in a variable in main() also called bookList
Close the editor, run the program; get this part to
run before proceeding.
Notice that these titles from the file are in alphabetical
order.
Since you read the strings from a file, each one will contain
a new line at the end so you will see a blank line
after each one. We will get rid of these next.
- Write the definition of a function called removeNewLines()
that takes one parameter, bookList (your list of titles)
and removes the newline character from the end of each
element in the list. An example of doing this is
also in hw4Files.py
(remember that the readlines() function keeps the newline at the
end of the string).
- Place another call to printList() after the call
to removeNewLines. The list should now appear
as one string per line with no blank lines in between.
Close the editor, run the program; get everything up to here working
before proceeding.
- Next in your main function definition,
use a while loop to allow the user to enter new titles, appending them to the
list of titles from the file.
Ask the user each time if s/he wants to add
another title? The while loop condition will check to see
if the answer is "yes" or "Yes".
When the while loop condition is true, you will
prompt the user to enter the next title.
Suppose you store the next title in a variable called
nextBook. Then use the append() method to append this to
your bookList. For example:
bookList.append(nextBook)
(If you want to read about this a little bit, read page 343 of the textbook).
- After the while loop, place another call to
your function printList() so you can see the list as it is
with the user's new titles appended to it. Make sure to exit the
editor, run the program, and get everything working to here
before proceeding.
-
Now write a function that sorts the titles. You can directly copy
the function called selSort() on page 444. In this example,
selSort() sorts a list of numbers into increasing order.
Name your function selSort() and call its parameter books instead
of nums. You'll have to replace every occurrence of
nums in the definition with books. But otherwise the
statements are all the same.
books is a list of strings, but the < operator works
on strings in Python, so the function as written will check whether a string is
less than another string, according to the ASCII values of
each character in the string.
- In your definition of main(), place a call to the function selSort() after the while
loop that prompts the user for more titles.
- Place a call to the function printList() next, after
the call to selSort() in your main function definition,
so you and the user can verify that the bookList is sorted
and includes all of the titles the user just entered.
-
Finally you will define a function called writeBooks() that writes
all of the list elements back out to the same file, "books.dat".
It will take in two parameters. The first is the bookList
to be written out, and the second is a file parameter
called bookFile.
The titles must be stored exactly
the same way as they were when your program first read them from the file.
Do not introduce blank lines in the file. However,
there should be only one book title per line.
That way your same program can access the file again in the future.
- At the end of your main() function definition, open
the file books.dat in write mode. Call it outfile.
Then place a call to function writeBooks(), passing it the
two parameters it needs.
Note 1: when debugging, examine your file books.dat. If it has strange
data in it, delete
it and put the old data back in, or just do
getcopy books.dat
to overwrite your incorrect one.
Note 2: In all the homeworks from now on, including this one, you
must provide documentation for all functions.
i.e. Just before the function definition, include a
banner, in comments, that says the function name,
what it takes for parameters and what it returns,
and what task it performs.
Note 3: I placed a plain
print
statement before each call to printList() in my main() definition
to separate each time the list of books is printing.
What to submit. Your program should be in a file called hw7.py. Submit this homework by the due date with the command submit homework7 hw7.py