On Jul 14, 1:10 pm, Duncan Booth wrote:
> John Machin wrote:
> > Try an iterative version of checking that () [] and {}
> > are balanced and nested appropriately.
>
> Here's how I might approach the more general case:
>
> def balanced(s, parens=("()",)):
> '''
> Example:
> >>> balance
John Machin wrote:
> Try an iterative version of checking that () [] and {}
> are balanced and nested appropriately.
Here's how I might approach the more general case:
def balanced(s, parens=("()",)):
'''
Example:
>>> balanced('aAAA(b[bb(c]c))')
True
>>> balanced('aAAA(b[bb(
Adrian Dziubek gmail.com> writes:
> In the i() there should be "return
> op == 0" on the end.
Hard to tell. In the absence of any docs, we have to resort to divination on the
function name :-P ... is "i" short for "iterative" or "imbalanced"?
--
http://mail.python.org/mailman/listinfo/pytho
On Jul 14, 11:08 pm, David Smith wrote:
> Jeremy Sanders wrote:
> > candide wrote:
>
> >> I'm trying to implement in Python a function testing if an expression is
> >> well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik"
> >> is correctly parenthesized but this one "zx(4er(1(er
candide free.invalid> writes:
> # The obvious iterative version
> def i(s):
> op = 0 # op : open parenthesis
> for k in range(len(s)):
> op += (s[k] == '(') - (s[k] == ')')
> if op < 0: break
> return op
>
E: H c, w t P.
F: A c, b à P.
Suggested better code:
def it
Martin P. Hellwig wrote:
candide wrote:
To add to your implementations; a readable version:
+++file parantheses.py+++
"""Parentheses Module Test"""
def parentheses_are_paired(input_string):
"Check if 'input_string' contains paired parentheses, if so return
True."
parenthesis_count = 0
Jeremy Sanders wrote:
> candide wrote:
>
>> I'm trying to implement in Python a function testing if an expression is
>> well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik"
>> is correctly parenthesized but this one "zx(4er(1(er(Yy)ol)ol)ik" is not.
>>
>> My code follows at the
Diez B. Roggisch wrote:
> Yep, you are:
>
> "(((("
>
> is certainly not "well parenthized".
Thanks for that!
--
Jeremy Sanders
http://www.jeremysanders.net/
--
http://mail.python.org/mailman/listinfo/python-list
candide wrote:
To add to your implementations; a readable version:
+++file parantheses.py+++
"""Parentheses Module Test"""
def parentheses_are_paired(input_string):
"Check if 'input_string' contains paired parentheses, if so return
True."
parenthesis_count = 0
parenthesis_open = '(
> Don't you want to just test that the number of "("s equals the number of
> ")"s or am I missing the point?
I had this idea too, but there is additional requirement that any
beginning must have greater or equal number of '(' than ')'.
--
Adrian
--
http://mail.python.org/mailman/listinfo/python-li
Jeremy Sanders wrote:
> candide wrote:
>
>> I'm trying to implement in Python a function testing if an expression is
>> well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik"
>> is correctly parenthesized but this one "zx(4er(1(er(Yy)ol)ol)ik" is not.
>>
>> My code follows at t
Strings are immutable, so your method of slicing one letter at time
will be building lots of them. That shouldn't hurt you here, but it
will when you hit a bigger problem. In the i() there should be "return
op == 0" on the end.
def well(expr):
mapping = {'(':1, ')':-1}
count = 0
for s in exp
candide wrote:
> I'm trying to implement in Python a function testing if an expression is
> well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik"
> is correctly parenthesized but this one "zx(4er(1(er(Yy)ol)ol)ik" is not.
>
> My code follows at the end.
>
> If you have a bette
13 matches
Mail list logo