0

Balanced Parentheses-How do you think I could write this code better?

##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. def balanced(expression): while "(" not in expression or ")" not in expression: print("Please enter a in parentheses expression : ", end="") expression = input() lst = [] for i in expression: if i == "(" or i == ")": lst.insert(0, i) if lst[-1] == ")": return False lst = [] for i in expression: if i == "(": lst.insert(0, i) try: if i == ")": lst.pop(0) except IndexError: return False if len(lst) == 0 : return True else: return False print(balanced(input("Please enter a in parentheses expression : ")))

4th Jan 2022, 7:35 PM
Bekir
Bekir - avatar
4 Answers
+ 1
Yup, your program looks good in my opinion, nice job :) Well actually, I don't think you need the first loop at all. I think you are trying to check that the last parenthesis is a closing one, but it's not needed. What if it isn't? example: ())( Here in the third letter, you would be trying to pop from an empty stack which raises an IndexError. example: (())( Here, the balance at the end would not be 0. In other words, `len(lst) == 1`. So your program already does these checks, like whether it starts with a "(" and ends with a ")". The extra loop at the top is superfluous :)
5th Jan 2022, 11:22 AM
Schindlabua
Schindlabua - avatar
+ 1
If you only want to check for "(" and ")" you don't even need a stack. Set i = 0. If you read "(", then `i+=1`. This is like `lst.insert`. If you read ")", then `i-=1`. This is like `lst.pop`. If `i<0`, return false. This is like `IndexError`. At the end of the program, if `i != 0`, return False. This is like `len(lst) != 0`. else return True. Using a stack is important if you want to match many different pairs of parentheses like (), [], {}, <>, all at once.
5th Jan 2022, 1:43 AM
Schindlabua
Schindlabua - avatar
+ 1
Schindlabua you are right but the project is about stack so i coded it like this. but alternatively it's so cool in your opinion..I also need to check if the opening parenthesis precedes the closing parenthesis in the expression.
5th Jan 2022, 9:03 AM
Bekir
Bekir - avatar
+ 1
Schindlabua You're right. The first "for" loop was unnecessary. I also saw that in the "while" loop it should be "and" instead of "or". Even if the code works, I always need other ideas to come up with a better one. Thanks...
5th Jan 2022, 12:30 PM
Bekir
Bekir - avatar