[pyar] [OT] - Checkeando parentesis balanceados.

John Rowland Lenton john.lenton en canonical.com
Vie Jun 18 18:49:15 ART 2010


On Fri, Jun 18, 2010 at 05:43:02PM -0300, Roberto Alsina wrote:
> On Friday 18 June 2010 17:29:27 John Rowland Lenton wrote:
> > que tal así:
> > 
> >     def balanceados(caso):
> >         try:
> >             re.compile(caso)
> >         except re.error:
> >             return False
> >         else:
> >             return True
> 
> No necesitás re:
> 
> def balanceado(caso):
> 	try:
> 		eval(caso)
> 		return True
> 	except SyntaxError:
> 		return False

eso falla con "()()", que entendí que estaba bien.
Además,

$ python -m timeit -s 'from q import f_eval, f_re' 'f_eval("(())")'
100000 loops, best of 3: 8.9 usec per loop
$ python -m timeit -s 'from q import f_eval, f_re' 'f_re("(())")'
100000 loops, best of 3: 1.24 usec per loop

además además,

$ python -m timeit -s 'from q import f_eval, f_re' 'f_re("("*100 + ")"*100)'
1000 loops, best of 3: 1.72 msec per loop
$ python -m timeit -s 'from q import f_eval, f_re' 'f_eval("("*100 + ")"*100)'
s_push: parser stack overflow
s_push: parser stack overflow
s_push: parser stack overflow
s_push: parser stack overflow
s_push: parser stack overflow
s_push: parser stack overflow
s_push: parser stack overflow
s_push: parser stack overflow
s_push: parser stack overflow
s_push: parser stack overflow
[snip]

y tomá mate.
------------ próxima parte ------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: Digital signature
URL: <http://listas.python.org.ar/pipermail/pyar/attachments/20100618/31687ada/attachment.sig>


More information about the pyar mailing list