+ 1

Why the code das not work for one case

I wrote code for a challenge in the data structures course and the code works good for all the cases except one. This is the code: def balanced(expression): #your code goes here open = 0 close = 0 expression = list(expression ) for c in expression: if c == "(": open += 1 elif c == ")": close += 1 if close == open : return True else : return False print(balanced(input())) (I need to find duplicate parentheses)

12th Feb 2022, 7:50 PM
yoel
3 ответов
+ 5
Suppose the test case was ")(" – your code would return True but it should return False
12th Feb 2022, 7:54 PM
Lisa
Lisa - avatar
+ 5
yoel , please read the task description carefully. it gives some very helpful hints how this can be implemented in detail happy coding!
12th Feb 2022, 8:14 PM
Lothar
Lothar - avatar
0
Thanks Lisa i recognise what was my mistake and fixed it
12th Feb 2022, 8:31 PM
yoel