Balanced Parentheses python problem | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Balanced Parentheses python problem

I made another way to solve the problem in data structures and when running the code in the final project the last test become a fail and it is hidden so I want to know why my code has the error with my sure, it is very good. this is the question and I achieve the problem but I want to change the way: 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 and my new solution is: openParenthesis = ['[', '{', '('] closeParenthesis = [']', '}', ')'] # the number of close Parentheses must be an equal number of the open Parenthesis def check(inp = input()): stackOpen = [i for i in inp if i in openParenthesis ] stackClose = [c for c in inp if c in closeParenthesis ] if len(stackClose) == len(stackOpen): print(True) else: print(False) check() #if you want to know about the true solution this is the code: openPranthesis = ['[', '{', '('] closePranthesis = [']', '}', ')'] def balanced(check): liStack = [] for i in check: if i in openPranthesis: liStack.append(i) elif i in closePranthesis: pos = closePranthesis.index(i)

29th Aug 2021, 8:18 PM
Mohammad Jamal Mahmoud Al Jadallah
Mohammad Jamal Mahmoud Al Jadallah - avatar
2 Antworten
0
Test every character. Start set balance = 0. If char == ”(” increase balance with +1, else if it is ”)” decrease balace with -1. But if balance now < 0 then its not balanced, so you can break the loop. Orherwise when you have traversed all characters, the balance should be zero, otherwise they are not balanced.
29th Aug 2021, 9:41 PM
Per Bratthammar
Per Bratthammar - avatar
0
Try with this code , easier to understand with a simple python language def balanced(expression): #your code goes here open = ["[","(","{"] close = ["]",")","}"] stack = [] for i in expression: if i in open: stack.append(i) elif i in close: pos = close.index(i) if len(stack)>0 and pos == open.index(stack[-1]): stack.pop() else: return False else: continue if len(stack) == 0: return True else: return False #(x+y)*(x-z)-9 print(balanced(input()))
25th Jan 2022, 4:13 PM
Ihsan Ayyach
Ihsan Ayyach - avatar