Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. class Stack:
  2. def __init__(self):
  3. self.storage = []
  4.  
  5. def is_empty(self):
  6. return len(self.storage) == 0
  7.  
  8. def push(self, value):
  9. self.storage.append(value)
  10.  
  11. def pop(self):
  12. return self.storage.pop()
  13.  
  14. def top(self):
  15. return self.storage[-1]
  16.  
  17.  
  18. def well_matched(string_):
  19. opening = ['(', '{', '[']
  20. closing = [')', '}', ']']
  21. open_stack = Stack()
  22. for ch in string_:
  23. if ch in opening:
  24. open_stack.push(ch)
  25. else:
  26. if open_stack.is_empty():
  27. return 'NO'
  28. top = open_stack.top()
  29. if opening.index(top) != closing.index(ch):
  30. return 'NO'
  31. open_stack.pop()
  32. if open_stack.is_empty():
  33. return 'YES'
  34. return 'NO'
  35.  
  36.  
  37. if __name__ == '__main__':
  38. n = int(raw_input())
  39. for i in range(n):
  40. string_ = raw_input()
  41. print well_matched(string_)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement