0
Balanced Parentheses
Hello ladies and gentelmen What is wrong with this input def balanced(expression): expression = list(expression.split()) try: for i in expression: if i == '(': expression.insert(0, i) elif i == ')': expression.pop(0, i) finally: if len(expression) == 0: return True elif len(expression) > 0: return False print(balanced(input()))
15 Respuestas
+ 5
I see the most of answers going to define own class, that can ofcourse. but there is a simple way to do the same thing by using list class in python , i used the code below and it works :
def balanced(expression):
nn = []
for char in expression:
if char == '(':
nn.insert(0, char)
elif char == ')':
if len(nn) == 0:
return False
nn.pop()
return len(nn) == 0
print(balanced(input())
+ 4
Consider a case string starts with ")"
Now we know paranthese aren't balanced
But your code will not do anything
Hope It Helps You 😊
+ 3
At least ur trying
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.insert(0, item)
def pop(self):
return self.items.pop(0)
s = Stack()
inp = input()
def balanced(inp):
try:
for i in inp:
if i == str("("):
s.push('(')
elif i == str(")"):
s.pop()
else:
continue
if len(s.items) == 0:
print(" True")
else:
print("False")
except:
print("False")
balanced(inp)
+ 3
@Charan Manikanta Nalla
From begin :
For every turn
if you found "(" put it in list .
If you found ")" delet the last "(" from the list , => if list is empty it means i got a ")" without "(" so break down and the result is False .. its not balanced . there is no need to check any more . That why i put => return : False .
if list is not empty it mean's i have at least one "(" so i will delete it and so on
+ 1
Still struggling
+ 1
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.insert(0, item)
def pop(self):
try:
return self.items.pop(0)
except:
par.push(")")
def is_empty(self):
if self.items == []:
return True
else:
return False
prompt = input()
par = Stack()
for c in prompt:
if c == "(":
par.push("(")
elif c == ")":
par.pop()
print(par.is_empty())
+ 1
@Murtada abed
I couldn't quite understand the "return False" part.
Can you explain that a little bit, please?
+ 1
pop expected only one argument
0
Sandeep Kushwaha
I have tried that no i dont get it
Demostrate what you are taking about it.
0
Looser what does “try” in your code do? I cant remeber learning it here
0
Welp, this is what worked for me. The "except IndexError:" is what solved the issue of having any extra ")" at the beginning of the expression.
def balanced(expression):
cache = []
try:
for char in expression:
if char == "(":
cache.insert(0,"(")
elif char == ")":
cache.pop(0)
except IndexError:
return False
if cache == []:
return True
else:
return False
print(balanced(input()))
0
def balanced(expression):
#your code goes here
stack = []
for b in expression:
if b == "(":
stack.insert(0,b)
elif b == ")" and stack != []:
stack.pop()
elif b == ")" and stack == []:
stack.insert(0, b)
return len(stack) == 0
print(balanced(input()))
0
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.insert(0, item)
def pop(self):
self.items.pop()
def list_stack(self):
return self.items
s = Stack()
z = input()
def push_bal(x):
for i in x:
if i == '(':
s.push(i)
return s.list_stack()
def pop_bal(x):
for i in x:
if i == ')':
s.pop()
return s.list_stack()
def check_bal(z):
if ')' and '(' not in z:
print(False)
else:
try:
push_bal(z)
pop_bal(z)
except :
print(False)
else:
L = s.list_stack()
print(len(L) == 0)
check_bal(z);
0
Actually this code donot pass one of the tests. Please let me know . I am not using a pro version so my mistake is hidden.