[Tutor] Help with Homework ;)

ak@silmarill.org ak@silmarill.org
Mon, 09 Jul 2001 21:12:36 -0400


On Mon, Jul 09, 2001 at 06:00:49PM -0700, Bob Rea wrote:
> I am trying to teach myself Python from Wes Chun's book and I am 
> stuck on one of the exercises, 3-11. I could use a hint.
> 
> Here is what I have tried (the exercise is  stated in the comments):
> 
> #!/usr/bin/env python
> '''
> $Id: fgrepwc.py,v 1.3 1999/09/08 02:23:51 wesc Exp wesc $
> 
> fgrepwc.py - searches for string in text file
> 
>     This module will output all lines of given file containing given 
> string,
>     including total number of matching lines.  This module can be 
> imported,
>     or executed standalone with the following usage syntax:
> 
>     Usage:  fgrepwc.py word filename
> 
> Exercises:
> 
>     3-10) add case-insensitive search
> 
>     3-11) change "line count" to "word count...," i.e., check if word
>         appears more than once in line and truly count how many times
>         a word shows up in a text file
> '''
> 
> import sys        # system module
> import string        # string utility module
> 
> #
> # usage() -- prints usage and exits
> #
> def usage():
>     print "usage:  fgrepwc [ -i ] string file"
>     sys.exit(1)
> 
> 
> #
> # filefind() -- searches for 'word' within file 'filename'
> #
> 
> 
> def filefind(word, filename):
> 
>     # reset word count
>     count = 0
> 
>     # see if we can open the file
>     try:
>         file_handle = open(filename, 'r')
>     except:
>         print filename, ":", sys.exc_value[1]
>         sys.exit(1)
> 
>     # read in all lines and close file
>     lines = file_handle.readlines()
>     file_handle.close()
> 
>     # for each line, check for the word
>     for i in lines:
>         if sys.argv[1] == '-i':
>              if string.count(string.split(string.lower(i)), word) > 
> -1:

why are you splitting i? string.count takes two strings, in this case
it should be something like string.count(i.lower(), word). When you split
i you get a list of words there, instead of one string.

Also, if you use 2.1 (and maybe 2.0), it's shorter to use string methods
like i.lower() instead of string.lower(i).

>                 count = count + 1
>                 print i,
>         else:
>             if string.count(string.split(i), word) > -1:
>                 count = count + 1
>                 print i
> 
>     # output line count
>     print count
> 
> 
> #
> # main() -- used only when module is executed
> #
> def main():
> 
>     # check command-line arguments
>     print "Hello"
>     print sys.argv
>     argc = len(sys.argv)
>     if argc != 3 and argc != 4:
>         usage()
>     if sys.argv[1] == '-i':
>         filefind(sys.argv[2], sys.argv[3])
>     else:
>         filefind(sys.argv[1], sys.argv[2])
> 
> 
> 
> 
> # executes only if not imported
> if __name__ == '__main__':
>     main()
> 
> comments?
> 
> -- 
> Bob Rea
> 
> 	Fear of Hell is pernicious;
>                 So is fear of Heaven.
> 
> rear@sirius.com   http://www.sirius.com/~rear
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: http://silmarill.org/cymbaline