[pyar] [OT] - Checkeando parentesis balanceados.
Sebastian Bassi
sbassi en clubdelarazon.org
Vie Jun 18 15:21:52 ART 2010
2010/6/18 Mariano Garcia Berrotarán <garcia.berrotaran en gmail.com>:
> la funcion tiene que retornar un booleano representando si los
> parentesis estan balanceados o no.
Mi versión es facil de entender y pasa tus tests:
import unittest
class balanceTestCase(unittest.TestCase):
def testSimple(self):
self.assertEquals(balance("()"), True)
def testOnion(self):
self.assertEquals(balance("(((((())))))"), True)
def testInverted(self):
self.assertEquals(balance(")("), False)
def testBroken(self):
self.assertEquals(balance("())(()"), False)
def balance(s):
cont = 0
for x in s:
if cont<0:
return False
else:
cont = cont-1 if x == ')' else cont+1
return True if cont == 0 else False
suite = unittest.TestLoader().loadTestsFromTestCase(balanceTestCase)
unittest.TextTestRunner(verbosity=2).run(suite)
More information about the pyar
mailing list