Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. #to check wheather the given parenthesis are valid or not implementation is done using stack
  2. pat = '[({()})[]}'
  3. lst = []
  4. srt_pat = ['{', '(', '[']
  5. cls_pat = ['}', ')', ']']
  6. chec_lst=['{}','()','[]']
  7. for i in range(len(pat)):
  8. if (pat[i] in srt_pat):
  9. lst.append(pat[i])
  10. else:
  11. x=lst[-1]+pat[i]
  12. if (x in chec_lst):
  13. lst.pop()
  14. else:
  15. print('not valid pat')
  16. break
  17. print(lst)
  18. if (len(lst) == 0):
  19. print('valid pat')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement