Trying to solve balanced parenthesis in python data structure[solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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))

30th Mar 2021, 2:46 PM
Simisola Osinowo
Simisola Osinowo - avatar
4 Answers
+ 3
Try to implement it with stack operation
30th Mar 2021, 3:38 PM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
0
Sorry can you show a practical example
30th Mar 2021, 3:46 PM
Simisola Osinowo
Simisola Osinowo - avatar
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
31st Mar 2021, 3:09 AM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
0
I still could not get it
31st Mar 2021, 10:59 AM
Simisola Osinowo
Simisola Osinowo - avatar