+ 2
Trying to solve balanced parenthesis in python data structure[solved]
Here is my code,only three test cases seem to come out right def com(lst,char): if len(lst)>0: for i in lst: if i in lst[-1]: if i =="(" and char==")": return True if i =="{" and char=="}": return True if i=="[" and char=="]": return True else: return False if len(lst)==0: return False lst=["(","[","(]"] char=")" com(lst,char) print(com(lst,char))
4 Respuestas
+ 3
Try to implement it with stack operation
0
Sorry can you show a practical example
0
Stack operation is quite simple.
If you get a opening bracket then you just need to insert it at 0 index of a list
and
if you get a closing brackets then first you need to check if wether the list is empty or not. If empty then return False as we are going to pop(remove) a opening bracket from the list.
For every closing parenthesis we are deleting a opening parenthesis, at last by this way either we get a empty list(all parenthesis are perfectly closed) or non-emplty list(means an opening parenthesis is remaining)
(AND IN THIS ONLY SIMPLE BRACKETS "()" ARE USED, SO YOU DON'T NEED TO USE FOR CURLY AND SQUARE BRACKETS)
You can look at this and try to implement on your own
https://code.sololearn.com/c1jG7l2BH3zp/?ref=app
0
I still could not get it