Balanced Parentheses | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Balanced Parentheses

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 ---------------------------------------- I tried to solve it several times but did not find answers to all of the cases. Can someone solve it for me??

3rd Jul 2022, 10:06 PM
Ibrar ul Hassan
Ibrar ul Hassan - avatar
7 Answers
+ 2
Ibrar-ul- Hassan , please post your try here so that we can see where the issue is
4th Jul 2022, 2:36 PM
Lothar
Lothar - avatar
+ 1
Ibrar-ul- Hassan Pls edit your question description and include a link to your code in Code Playground. Use "+" button to add the link. It also helps if you add, also in the question description, an explanation of your difficulties.
4th Jul 2022, 5:12 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Jimmy Tyrrell 🇮🇳Mohammad Iqbal Pls avoid giving finished code as answer, because it makes the OP skip the most important part of learning. Also, it reinforces the bad habit of asking others to solve one's tasks. Prefer giving hints for the OP to find the solution instead.
20th May 2023, 10:00 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Jimmy Tyrrell your suggestion is valid, I will avoid sharing the full answer from now onwards.
21st May 2023, 5:00 AM
🇮🇳Mohammad Iqbal
🇮🇳Mohammad Iqbal - avatar
0
This is my solution. It uses an empty list to simulate a stack, and a loop to pull any opening parenthesis into the stack, then pop it back out when a closed parenthesis shows up. If anything is left once the loop is finished, it returns false. One of the hidden test cases starts with a closed parenthesis, so I had to put a condition in the loop that if a closed parenthesis shows up when the stack is empty, then it automatically stops the loop and returns false. https://code.sololearn.com/cJJ1C2xt1Ril/?ref=app
6th Jul 2022, 1:08 PM
Jimmy Tyrrell
Jimmy Tyrrell - avatar
0
##Hopefully this will surely works... def balanced(expression): #your code goes here 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) stack = Stack() if expression.count('(') == 0 or expression.count(')') == 0: return False if expression.count('(') == expression.count(')'): for i in range(len(expression)): if expression[i] == '(': stack.push(i) if ")" in expression[i:]: stack.pop() if(stack.is_empty()): return True else: return False else: return False print(balanced(input()))
20th May 2023, 1:35 AM
🇮🇳Mohammad Iqbal
🇮🇳Mohammad Iqbal - avatar