+ 9

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

9th Mar 2021, 6:41 PM
Qusi AL-Hejazi
+ 8
# Function to test balanced brackets def BalancedBrackets(Str): # stack for storing opening brackets stack = [] # Loop for checking string for char in Str: # if its opening bracket, so push it in the # stack if char == '{' or char == '(' or char == '[': stack.append(char) # push # else if its closing bracket then # check if the stack is empty then return false or # pop the top most element from the stack # and compare it elif char == '}' or char == ')' or char == ']': if len(stack) == 0: return False top_element = stack.pop() # pop # function to compare whether two # brackets are corresponding to each other if not Compare(top_element, char): return False # lastly, check that stack is empty or not if len(stack) != 0: return False return True # Function to check two corresponding brackets # equal or not. def Compare(opening, closing): if opening == '(' and closing == ')': return True if opening == '[' and closing == ']': return True if opening == '{' and closing == '}': return True return False # Test function exp=input() print(BalancedBrackets(exp)) #print(BalancedBrackets("{123(456[.768])}"))
16th May 2021, 9:02 AM
Habibullah
+ 2
brackets=[] for char in expression: if char is openingbracket: push it to brackets elif char is closingbracket pop brackets or False brackets should be an empty list now
9th Mar 2021, 7:09 PM
Oma Falk
+ 2
Well, Goto leetcode(webpage) and submit your solution ,if failed try again.... Stack..= > Here it is, Just ignore all stuff rather '('[open parenthesis] and ')' [close parenthesis] so First : you need create a loop first ,which will terminate when you reach at the end of your string... if '(' encountered push it in your stack;;; if ')' encountered pop from your stack only if it is not empty... else it's just imbalanced as there is no '(' to match ')' so just return false ;;; after the end of loop check your stack, if it is empty it means all parenthesis are balanced return true else return false.... Well,It's you one who should implement it....
10th Mar 2021, 9:59 AM
000
+ 2
12th May 2021, 6:12 AM
A͢J
+ 2
#Right and here 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:42 PM
MD: MOSTOFA
+ 1
Qusi AL-Hejazi Use Push and Pop technique.
9th Mar 2021, 6:44 PM
A͢J
+ 1
The code doesn't run please give a correct code
12th May 2021, 5:35 AM
Muthu kumar.V
+ 1
I tried it but code doesn't run.it indicates some errors ☹️
12th May 2021, 5:45 AM
Muthu kumar.V
+ 1
When u call pila.pop() u must check the return If it is False u must return False
20th Jan 2022, 5:15 PM
Oma Falk
+ 1
def balanced(expression): #your code goes here li=[] flag=0 for i in expression: if len(li)==0 and i==")": flag=1 elif i==")": li.remove("(") elif i=="(": li.append("(") if len(li)==0 and flag==0: return "True" else: return "False" print(balanced(input()))
1st Sep 2022, 11:24 AM
Aakib Khan
0
Muthu kumar.V Why you don't try self to write a code?
12th May 2021, 5:42 AM
A͢J
0
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()))
2nd Oct 2021, 8:55 AM
Hariom Kumar
0
Hi all! I am starting in the world of programming and I am not finding the error in my code. Of seven cases tested, only one gives an error and since I am not using the paid version it does not let me see the input to see what I can correct. I hope you can help me. Thanks a lot 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): if self.items != []: return self.items.pop(0) else: return False def print_stack(self): print(self.items) def balanced(expression): pila = Stack() for i in expression: if i == "(": pila.push("(") elif i == ")": pila.pop() if pila.items == []: return True else: if pila.items != []: return False print(balanced(input()))
20th Jan 2022, 3:29 PM
Gustavo Gonzalez
0
Thank you very much for the help!
22nd Jan 2022, 3:38 AM
Gustavo Gonzalez
0
here is my code: #your code goes here value = [] for i in expression: if i == "(": value.insert(0, i) elif i == ")": if len(value) == 0: return("False") elif len(value) != 0: value.pop(0) if value.count("(") == value.count(")"): return("True") elif value.count("(") != value.count(")"): return("False")
3rd Dec 2022, 2:31 PM
Yen Chun Chen