0
Balanced Parantheses - Python Data Strcutures
Hi, My code is failing Test Case #3 and Test Case #7, can someone help why? I have tried a lot but can't seem to figure out what's going wrong. Any help will be highly appreciated. 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 balanced(expression): #your code goes here x=Stack() for i in expression: if i=='(': x.push(i) elif i==')': x.pop() return x.is_empty() print(balanced(input()))
4 ответов
+ 2
# simple fix:
def balanced(expression):
#your code goes here
x=Stack()
for i in expression:
7
if i=='(':
x.push(i)
elif i==')':
if x.is_empty(): return False
else: x.pop()
return x.is_empty()
0
you posted twice the same thread/question:
https://www.sololearn.com/Discuss/2821562/?ref=app
https://www.sololearn.com/Discuss/2821561/?ref=app
0
your code throw error if stack is empty and closing parenthesis is encountered (try to pop empty items list ^^)