[Tutor] pattern matching problem

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu May 26 22:55:47 CEST 2005



On 26 May 2005 cgw501 at york.ac.uk wrote:

> One of the worst I think was doing loads of real spazzy stuff trying to
> split whole files in to lists of letters and use string methods to find
> the first uppercase one.

Hi Chris,

An approach like this might work.  Rather than read the whole thing into a
honking big list, though, we can just iterate through it, letter by
letter, by using read(1).  Here's one way we might do it that way:

### Pseudocode ###
currentLine = 1
while True:
   nextChar = someFile.read(1)
   if not nextChar:
       break
   elif isNewline(nextChar):
       currentLine += 1
   elif isUppercased(nextChar):
       break
print currentLine
######

This approach avoids sucking the whole file into memory, and is a
reasonable approach too.


Best of wishes!



More information about the Tutor mailing list