+ 2

Balanced Parentheses Run. Do anyone know answer for this? I am getting wrong answer for two hidden cases.

Parentheses are balanced, if all opening parentheses have their corresponding closing parentheses. Given an expression as input, we need to find out whether the parentheses are balanced or not. For example, "(x+y)*(z-2*(6))" is balanced, while "7-(3(2*9))4) (1" is not balanced. The problem can be solved using a stack. Push each opening parenthesis to the stack and pop the last inserted opening parenthesis whenever a closing parenthesis is encountered. If the closing bracket does not correspond to the opening bracket, then stop and say that the brackets are not balanced. Also, after checking all the parentheses, we need to check the stack to be empty -- if it's not empty, then the parentheses are not balanced. Implement the balanced() function to return True if the parentheses in the given expression are balanced, and False if not. Sample Input: (a( ) eee) ) Sample Output: False

28th Apr 2021, 6:44 AM
Tejaswin J
Tejaswin J - avatar
14 Réponses
+ 1
Share your code and we can likely find cases it'll misbehave on. One case that breaks for a lot of people is: ) Make sure your code correctly indicates "False" for a single closing bracket. Your script shouldn't throw an exception or say "True".
28th Apr 2021, 2:26 PM
Josh Greig
Josh Greig - avatar
+ 9
Tejaswin, the following is a bit simpler and based on the observation that the stack of only "(" characters is nothing but a glorified counter. def balanced(expression): count = 0 for char in expression: if char == "(": count += 1 elif char == ")": if count == 0: return False count -= 1 return count == 0 print(balanced(input()))
13th May 2021, 1:15 AM
Josh Greig
Josh Greig - avatar
+ 6
Tejaswin wrote, "I have attached the code below. ..." Response: This case should return True but instead you get False: (1+1) I changed one line below and commented it to pass all test cases: def balanced(expression): stack = [] for char in expression: if char in ["("]: stack.append(char) elif char == ")": # replace else with this and your code should work. if not stack: return False current_char = stack.pop() if current_char == '(': if char != ")": return False if stack: return False return True print(balanced(input()))
1st May 2021, 8:43 PM
Josh Greig
Josh Greig - avatar
+ 1
Guys my code is failing in only one test case. https://code.sololearn.com/coV3HUJR26xe/?ref=app
14th May 2021, 6:46 PM
S.Raghunath
S.Raghunath - avatar
+ 1
DENIZ MUSTAFAOGLU, ")(" should not pass but your code would. "()" should pass. There are an infinite number of other cases like "())(" which should fail too when your function returns True.
6th Sep 2021, 4:07 PM
Josh Greig
Josh Greig - avatar
+ 1
Test case 1 fail but test case 2 passed where is the mistake #your code goes here class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items)-1] def size(self): return len(self.items) def balanced(expression): a = Stack() for i in expression: if i == "(": a.push(i) for i in expression: if i == ")": if a.size() == 0: return False else: a.pop() if a.size() == 0: return True else: return False print(balanced(input()))
17th Dec 2021, 9:21 AM
BRAHMA KUMARI MALLELA
+ 1
## Correcr answer def balanced(expression): count = 0 for char in expression: if char == "(": count += 1 elif char == ")": if count == 0: return False count -= 1 return count == 0 print(balanced(input()))
31st May 2022, 3:04 PM
A R P Amarasinghe
+ 1
def balanced(expression): #your code goes here listt = [] for i in expression: if i == '(' or i == ')': listt.insert(0, i) right = 0 left = 0 for j in listt: if j == '(': right+=1 if j == ')': left+=1 if right==left and listt[0] == ')' and listt[-1] == '(': return True else: return False print(balanced(input()))
22nd Sep 2022, 1:29 PM
Osama Omar Gaweesh
0
#Correct answer def balanced(expression): count = 0 for char in expression: if char == "(": count += 1 elif char == ")": if count == 0: return False count -= 1 return count == 0 print(balanced(input()))
22nd Jun 2021, 11:43 PM
MD: MOSTOFA
MD: MOSTOFA - avatar
0
Why does not work for Test Case 7? def balanced(expression): a = expression.count("(") b = expression.count(")") if a == b: return True else: return False print(balanced(input()))
8th Jul 2021, 5:50 PM
DENİZ MUSTAFAOĞLU
DENİZ MUSTAFAOĞLU - avatar
0
#Correct answer def balanced(expression): #your code goes here l=[] a=b=0 for i in expression: if i=="(": l.insert(0,i) a+=1 elif i=="(": l.insert(0,i) b+=1 if l.pop(0)==")" and a==b: return True else: return False print(balanced(input()), end='')
31st Aug 2022, 1:01 AM
Mohamad Eskandari Nasab
Mohamad Eskandari Nasab - avatar
0
The problem can be solved using a stack. class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items)-1] def size(self): return len(self.items) def balanced(expression): a = Stack() for char in expression: if char == "(": a.push(char) elif char == ")": if a.size() == 0: return False a.pop() return a.size() == 0 print(balanced(input()))
7th Nov 2022, 10:43 AM
Kuang Hangdong
Kuang Hangdong - avatar
0
n=input() c=[] for x in n: if x=='(' or x==')': c.append(x) if len(c)==0 or len(c)%2==1 or c.count("(")!=c.count(")"): print("False") else: while len(c)>2: for y in c: if y==")": if c.index(y)==0: print("False") break else: c.remove(y) c.remove(c[c.index(y)-1]) if c==[')', '(']: print("False") else: print("True")
14th Mar 2023, 10:46 AM
Jahanshahi Rayeni, Majid
Jahanshahi Rayeni, Majid - avatar
- 1
I have attached the code below. Check this. It is having some problem in two test cases.. def balanced(expression): stack = [] for char in expression: if char in ["("]: stack.append(char) else: if not stack: return False current_char = stack.pop() if current_char == '(': if char != ")": return False if stack: return False return True print(balanced(input()))
30th Apr 2021, 6:47 AM
Tejaswin J
Tejaswin J - avatar