knerr cs21 notes...
back to schedule
WEEK07: File I/O, top-down design
---------------------------------------------------------------
W: start top-down design, more file I/O
ANNOUNCEMENTS:
- Lab 6 due Thursday night
TOP-DOWN DESIGN:
Top Down Design is a problem solving technique where you:
1. Start with General Description of Problem
2. Break it into several high-level steps
3. Iteratively break the steps into smaller steps until you have
steps that are easy to solve
- just like writing a paper...start with an outline, then fill
in the second-level details, and so on until you can start
writing each section/function
- for example, writing a wheel-of-fortune game might start
with something like this:
main: read phrases from file
pick one phrase for the game
play the game, with the chosen phrase
output the results
Each one of those could be a single function, or may need to
be broken down into multiple functions. If you think one of
the above should be a single function, sketch it out (what
arguments would it need, what would it do and how, and what
should it return). If you think one of the above (ex: play the
game) will be more than one function, do another level of
top-down design on that.
YOUR TURN:
- Let's try using top-down design on a "wordcount" program.
There's a unix program called wc that tries to count the
number of lines, words, and characters in a file. Here's
an example of it:
$ wc /usr/local/doc/GettysburgAddress
25 266 1441 /usr/local/doc/GettysburgAddress
The wc program says there are 25 lines in that file, 266 "words",
and 1441 characters.
Can we write our own wordcount program???
Here's one way to structure the main function:
read in a file
count the number of lines
count the number of "words"
output the results
Now let's refine each of the above steps:
read in a file
takes one parameter: the file name to open
what it should do: read each line from the
file and store it in a list
returns: the list of lines
Note: there are many other ways you could do this! Let's
try this and see if it works. Sometimes I write
half a program before I realize it's not going to
work the way I've designed it...
that should be enough to write the first step:
def readFile(fname):
infile = open(fname, 'r')
lines = []
for line in infile:
lines.append(line.strip())
infile.close()
return lines
T E S T AS YOU GO!!!!
at this point, type in a simple main function and
make sure readFile works!
def main():
linelist = readFile("/usr/local/doc/GettysburgAddress")
print linelist
The next step in our top-down design is "count the number of lines".
We could have a function do this, but it's just a one-liner (using
len), so maybe we should just write it in main:
nlines = len(linelist)
The next step is count the number of "words". I keep putting
words in quotes because we are not really going to count the
number of english words -- we're just going to count the number
of "things" separated by white space. For example, both of
these lines have 5 "words" in them:
this is really quite fun
1 2 3 4 pizza
(really counting the number of valid English words can be done,
it's just more complicated)
Here's an example of using the str split method:
>>> s = "i love computer science"
>>> s.split()
['i', 'love', 'computer', 'science']
Can we use that and len to get the number of "words"?
Let's outline a countWords functions:
parameters: a list of lines
returns: number of "words" in the whole list of lines
outline: an accumulator that sums up the number of
words in each line
def countWords(llist):
count = 0
for line in llist:
n = len(line.split())
count = count + n
return count
Now we're almost done...just put it all together and make a
nice "output results" part:
def main():
filename = raw_input("file: ")
linelist = readFile(filename)
nlines = len(linelist)
nwords = countWords(linelist)
print "number of words = %d" % (nwords)
print "number of lines = %d" % (nlines)
IF YOU HAVE TIME:
- add a function to count the number of characters