Re: Python code for testing well parenthesized expression

2009-07-19 Thread Simon Forman
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

Re: Python code for testing well parenthesized expression

2009-07-14 Thread Duncan Booth
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(

Re: Python code for testing well parenthesized expression

2009-07-14 Thread John Machin
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

Re: Python code for testing well parenthesized expression

2009-07-14 Thread John Machin
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

Re: Python code for testing well parenthesized expression

2009-07-14 Thread John Machin
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

Re: Python code for testing well parenthesized expression

2009-07-14 Thread Martin P. Hellwig
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

Re: Python code for testing well parenthesized expression

2009-07-14 Thread David Smith
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

Re: Python code for testing well parenthesized expression

2009-07-14 Thread Jeremy Sanders
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

Re: Python code for testing well parenthesized expression

2009-07-14 Thread Martin P. Hellwig
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 = '(

Re: Python code for testing well parenthesized expression

2009-07-14 Thread Adrian Dziubek
> 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

Re: Python code for testing well parenthesized expression

2009-07-14 Thread Diez B. Roggisch
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

Re: Python code for testing well parenthesized expression

2009-07-14 Thread Adrian Dziubek
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

Re: Python code for testing well parenthesized expression

2009-07-14 Thread Jeremy Sanders
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

Python code for testing well parenthesized expression

2009-07-14 Thread candide
Hi, 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 better algorithm or a better P