+ 1

Balanced Parentheses solution question

I'm solving the final challenge in Python Data Structures. They ask for stack solution. But I can't seem to figure out why this solution wouldn't work. Why doesn't the following code solve the balanced parentheses? def balanced(expression): #your code goes here a = expression.count("(") b = expression.count(")") if "(" in expression and ")" in expression: if a == b and expression.find("(") < expression.find(")"): return True else: return False else: return "False" print(balanced(input()))

14th Feb 2023, 1:36 AM
max
3 Réponses
+ 4
Try this: (()))(
14th Feb 2023, 1:56 AM
Brian
Brian - avatar
+ 2
Oh, I see... It is a problem... Thank you, Brian!
14th Feb 2023, 3:07 AM
max
+ 1
You're welcome!
14th Feb 2023, 4:12 AM
Brian
Brian - avatar