0

what does this mean?? i can't understand??

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.

2nd Aug 2022, 3:32 AM
Deriz Les
Deriz Les - avatar
2 Answers
+ 1
Previous discussion bearing similar topics might help you out with hints, suggestions etc. It's always a good idea to search the forum first before posting "new" question. https://www.sololearn.com/Discuss/2688356/?ref=app https://www.sololearn.com/Discuss/2768639/?ref=app
2nd Aug 2022, 4:06 AM
Ipang
0
The parentheses should be equal number of opening ( and closing ). They can be nested and/or grouped, ((x)), (x(y)), (x)(y). Your implementation for balanced() would ignore anything that is not a parentheses and verify for every open there is a close. Due to nesting, you would set a variable to 0, add 1 for each open, subtract 1 for each close. If your var is ever < 0 or ends !=0, it is unbalanced.
2nd Aug 2022, 4:03 AM
Brandon
Brandon - avatar