Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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

26th Dec 2021, 9:01 PM
Jakhanov Sultanbek
2 Answers
+ 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
26th Dec 2021, 9:40 PM
Lisa
Lisa - avatar
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
27th Dec 2021, 12:26 AM
Jamari McFarlane
Jamari McFarlane - avatar