0
How to implement stack in python?
This is my attempt code in balanced input the last quiz for python intermediate.https://code.sololearn.com/c7qrPRYHlZHt/?ref=app but only the last test case is wrong.How to implement stack on that?
1 Odpowiedź
+ 4
your code only check for opening and closing parenthesis count equals... so return true for unbalanced ')(' in example ;)
the stack needed here is so basic that you could implement it with a string or a list, or even a basic counter...
initialize counter to 0
iterate over chars:
when encountered '(' increment counter
when encountered ')' check if counter == 0 then return false, else decrement counter
at end of iterations, return true if counter == 0 else return false