Python data structure last code project | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python data structure last code project

This is the last code project in the Python data structures course. This doesn’t work for the last locked case. It is to check if the parenthesis are balanced. My code: def balanced(exp): a = 0 b = 0 x = list(exp) for i in x: if i == "(": a += 1 elif i == ")": b += 1 if b == 1 and a == 0: return "False" if a == b: return "True" else: return "False" print(balanced(input())) Please help! Thanks!

2nd Mar 2022, 6:02 AM
Interesting
Interesting - avatar
2 Answers
+ 1
Try using a list as a stack. You're missing some edge cases. What if you had "())("? Your code would return True when this should be False.
2nd Mar 2022, 6:12 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Thank you so much :)
2nd Mar 2022, 6:23 AM
Interesting
Interesting - avatar