Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren0
Python
Implement the balanced() function to return True if the parentheses in the given expression are balanced, and False if not. def balanced(expression): #your code goes here a = expression.count("(") b = expression.count(")") if a == b: return True else: return False print(balanced(input())) 1-6 test case is working, but 7 is false
2 Antworten
+ 1
Consider this expression:
)(()
Your code would return True but it should return False.
edit:
look at the task description again, it gives you a hint to use a list and implement a stack to check the parentheses
0
given that what lisa said is the problem i'd say you could
store all of those symbols in a list
the first element + last element should give ()
if you reach a point where you can't its false