Balanced Parentheses Python DataStructures Project
Project: Parentheses are balanced, if all opening parentheses have their corresponding closing parentheses. Someone please help! I'm stuck in this test. There are a total of seven test cases. Five of them are correct but two of them are not going well in any way. Can anyone explain what's wrong with my code? Here's my code: 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 stack(self): return self.items def balanced(expression): s = Stack() for i in expression: if i == '(': s.push(i) if i == ')': if s.stack()[0] == '(': s.pop() else: s.push(i) if not s.stack(): return True else: return False