Balanced Parentheses
QUESTION : 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. MY TRY (USING STACK) : def areBracketsBalanced(expr): stack = [] for char in expr: if char in ["("]: stack.append(char) else: if char in ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'x', 'y', 'z', '+', '*', '-']: pass if not stack: return False current_char = stack.pop() if current_char == '(': if char != ")": return False if stack: return False return True if __name__ == "__main__": expr = input() if areBracketsBalanced(expr): print("True") else: print("False") WITHOUT STACK : def balanced(expression): openb = 0 closeb = 0 for i in expression: if i == '(': openb += 1 elif i == ')': closeb += 1 if openb == closeb: return True else: return False print(balanced(input())) i'm unable to get solve all the test cases