Balanced Parentheses
Someone can help me? I don't understand why this code doesn't run ok for all the inputs. Thaks!! class Stack: def __init__(self): self.items = [] def is_empty(self): return self.items == [] def push(self, item): self.items.insert(0, item) def pop(self): return self.items.pop(0) def print_stack(self): print(self.items) def is_match(p1, p2): if p1 == "(" and p2 == ")": return True else: return False def balanced(expression): s = Stack() balanced = True index = 0 while index < len(expression) and balanced: paren = expression[index] if paren in "(": s.push(paren) else: if paren in ")": s.pop(paren) else: if s.is_empty(): balanced = False else: top = s.pop() if not is_match(top, paren): balanced = False index += 1 if s.is_empty and balanced: return True else: return False print(balanced(input()))