Hi, I feel that the line13 made a mistake, when we cheking whether the top matches paren.
we should first execute top = s.peak()
if top matched the paren, then execute s.pop()
else, execute: is_balanced = False
s.pop() can not get the top element of stack
Hi yiwen,
This is Alina from Educative. Thank you for reaching out to us and giving your feedback.
We are incredibly happy to help you!
First of all, I can confirm that s.pop()
gets the top element of the stack. Now, what you suggest might also work but the current implementation is also correct. In both approaches, the is_balanced
is set to False
if top
does not correspond with paren
. This will help in breaking from the while
loop in case the brackets are not balanced. On the other hand, if top
corresponds with paren
, then it doesn’t matter whether you pop
the element before or after the matching condition. The outcome will be similar in both cases.
I hope this makes things clear.
We hope Educative has inspired you to further your learning! If you have any further concerns/questions/comments, please let us know.
Best Regards,
Alina Fatima | Developer Advocate
Educative.io
mam i am not able to import stack it shows module not found and i did it in python idle,so what should i do