Re: a little parsing challenge ☺
wxjmfauth at gmail.com
Wed Jul 20 08:29:56 CEST 2011
On 19 juil, 21:09, Terry Reedy <tjre... at udel.edu> wrote:
> On 7/19/2011 2:12 PM, Xah Lee wrote:
> >> Also, you may have answered this earlier but I'll ask again anyways: You
> >> ask for the first mismatched pair, Are you referring to the inner most
> >> mismatched, or the outermost? For example, suppose you have this file:
> >> foo[(])bar
> >> Would the "(" be the first mismatched character or would the "]"?
> > yes i haven't been precise. Thanks for brining it up.
> > thinking about it now, i think it's a bit hard to define precisely.
> Then it is hard to code precisely.
Not really. The trick is to count the different opener/closer
That is what I am doing to check balanced brackets in
chemical formulas. The rules are howerver not the same
as in math.
Interestingly, I fall on this "problem". enumerate() is very
nice to parse a string from left to right.
>>> for i, c in enumerate('abcd'):
... print i, c
But, if I want to parse a string from right to left,
what's the trick?
The best I found so far:
>>> s = 'abcd'
>>> for i, c in enumerate(reversed(s)):
... print len(s) - 1 - i, c
More information about the Python-list