[CentralOH] 2014-11-14 道場 Scribbles: Injection Attacks

Joe Shaw joe at joeshaw.org
Mon Nov 17 16:52:06 CET 2014


Hi,

I'd probably iterate over the string and increment a paren counter.  If it
ever went < 0, it's unbalanced and bail out. At the end, if it's > 0, it's
not properly closed.  This is basically a very lame lexer/parser state
machine. Something like:

for s in strings:
    paren_level = 0
    for i, c in enumerate(s):
        if c == '(':
            paren_level += 1
        elif c == ')':
            paren_level -= 1
        if paren_level < 0:
            print "{}: Unbalanced closing paren at pos {}: '{}'".format(s,
i, c)
            break
    # obviously would be better to track the position of the last open paren
    if paren_level > 0:
       print "{}: Unclosed opening paren somewhere".format(s)

Joe

On Mon, Nov 17, 2014 at 10:39 AM, Eric Floehr <eric at intellovations.com>
wrote:

> for s in strings:
>>     print(repr(s), s.count('(') == s.count(')'))
>>
>
> But that will return true for ")this) isn't balanced( b)ut (says it( is"
>
>
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> https://mail.python.org/mailman/listinfo/centraloh
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20141117/0cf751e8/attachment.html>


More information about the CentralOH mailing list