Scanning a file

Steve Holden steve at holdenweb.com
Sat Oct 29 17:43:18 EDT 2005


netvaibhav at gmail.com wrote:
> I think implementing a finite state automaton would be a good (best?)
> solution. I have drawn a FSM for you (try viewing the following in
> fixed width font). Just increment the count when you reach state 5.
> 
>                     <---------------|
>                    |                |
>         0      0   |  1      0      |0
>  -->[1]--->[2]--->[3]--->[4]--->[5]-|
>      ^ |    |      ^ |     |      |
>     1| |<---|      | |     |1     |1
>      |_|    1      |_|     |      |
>      ^             0       |      |
>      |---------------------|<-----|
> 
> If you don't understand FSM's, try getting a book on computational
> theory (the book by Hopcroft & Ullman is great.)
> 
> Here you don't have special cases whether reading in blocks or reading
> whole at once (as you only need one byte at a time).
> 
Indeed, but reading one byte at a time is about the slowest way to 
process a file, in Python or any other language, because it fails to 
amortize the overhead cost of function calls over many characters.

Buffering wasn't invented because early programmers had nothing better 
to occupy their minds, remember :-)

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list