HELP Newbie solve this Problem

Mark Hathaway hathawa2 at marshall.edu
Sat Mar 25 23:02:21 EST 2000


>>> On Fri, 24 Mar 2000 03:43:38 GMT, race9047 at my-deja.com declaimed the
>>> following in comp.lang.python:
>>>
>>>       I must be from a different world then... The second part is what
>>> I'd find easy... even in FORTRAN (maybe too easy in FORTRAN)

>> At 21:10 3/23/00 -0800, Dennis Lee Bieber wrote:
>>
>>       integer counts(26)      / 26 * 0 /      ! or is it 0 * 26?
>>       character       line*132        ! set for the longest line the
>>                                       ! the data file can contain
>>       open(10, file="data", status="old")
>>       read(10, '(a132)', iostat=ios) line
>>   10  continue
>>       if (ios .eq. 0) then
>>           do 20 i=1, 132              ! or the line length max
>>               if (line(i:i) .ge. 'a' .and. line(i:i) .le. 'z') then
>>                       inx = ichar(line(i:i)) - ichar('a') + 1
>>                       counts(inx) = counts(inx) + 1
>>               elseif (line(i:i) .ge. 'A' .and. line(i:i) .le. 'Z')
>>     x                                                 then
>>                       inx = ichar(line(i:i)) - ichar('A') + 1
>>                       counts(inx) = counts(inx) + 1
>>               else
>>                       continue        ! just my style
>>               endif
>>   20      continue
>>           read(10, '(a132)', iostat=ios) line
>>       goto 10
>>       endif
>>       close(10)
>>       do 30 i=1, 26
>>           print *, char(i), '     ', counts(i)
>>   30  continue
>>       stop
>>
>>
>>       It would look much cleaner in Python, and I'd use case
> >conversion modules rather than the duplicates in the IF statements...

I'm probably giving away somebody's homework, but I can't allow that
stupid Fartran to stand without comparison to Python.

Fortran 27 lines unreadable

Python  53 lines commented
        22 lines uncommented

---------------------------- snip here--------------------
class charFreq:

    # Written by: Mark Hathaway
    # Date: 2000.03.25
    #
    # This class allows one to read text from a file,
    # tally the frequency of each character in the text,
    # report the frequency of either all or only specified characters
    # and specifically ignore case - count uppercase A..Z as a..z.

    '''Usage:
       from freqchar import charFreq
       obj = charFreq()
       obj.getText("filename.ext")

       obj.reportCounts()
         It defaults to reporting on string.letters & ignoreCase = None

       obj.reportCounts(stringOfCharsToLookFor,ignoreCase)
         use obj.reportCounts("any letters","true") to give some
specific
         selection of letters and case specificity.'''

    def getText (self, filename):
        self.text = open(filename).read()
        self.charDict = {}   # to store each letter and occurrences of
it
        self.keys = []       # to store charDict.keys() - to sort later
        for char in self.text:
            if char not in self.keys:
                self.charDict[char] = 1
                self.keys.append(char)
            else:
                self.charDict[char] = self.charDict[char] + 1

    def reportCounts (self,
                      lookfor="abcdefghijklmnopqrstuvwxyz"+
                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
                      ignoreCase=None):

        '''One provides a character string and this method counts
the occurrences of each character of the string in the
text. If you're using the ignoreCase='true' option then
provide the lowercase in the lookfor character string
e.g.  obj.reportCounts('abcxyz')'''

        from string import uppercase, lower
        self.keys.sort()
        for char in lookfor:
            if ignoreCase and (char in uppercase):
                char = lower(char)
            if char in self.keys:
                print char, self.charDict[char]

#end charFreq



More information about the Python-list mailing list