+ 1

This code solves every case except for the last one that is hidden .

The problem was to write a code that checks if the parentheses in an expression are balanced , and to do that with a stack, I didn't do that because I had this much easier code in mind: text = input() def balanced(text): for i in text: if text.count("(") ==text.count(")"): return True else: return False print(balanced(text)) What to improve to solve the last case?

31st Jan 2022, 3:53 PM
Wellsie
Wellsie - avatar
3 ответов
+ 7
( ) ) ( Is it ok or not? Opening brackets= closing brackets
31st Jan 2022, 7:39 PM
Oma Falk
Oma Falk - avatar
+ 4
I remember having the same problem, maybe what that hidden case expects is that you use what the hint says, which is in fact the topic of the content being evaluated. "You can use a simple list for a stack. Use list.insert(0, item) to push on the stack, and list.pop(0) to pop the top item. You can access the top element of the stack using list[0]."
31st Jan 2022, 4:36 PM
CGM
CGM - avatar
+ 3
True, I changed my code based on what they asked for and it worked
31st Jan 2022, 4:42 PM
Wellsie
Wellsie - avatar