Advertisement
Guest User

Untitled

a guest
Dec 31st, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. class Stack:
  2.     def __init__(self):
  3.         self.items = []
  4.     def isEmpty(self):
  5.         return self.items == []
  6.     def push(self, item):
  7.         self.items.append(item)
  8.     def pop(self):
  9.         return self.items.pop()
  10. posible = True
  11. cin = input()
  12. a=Stack()
  13. s=Stack()
  14. for i in cin:
  15.     if ( i == '(' or i == '[' or i == '{' ):
  16.         s.push( i )
  17.  
  18.     if ( i == ')' or i == ']' or i == '}'):
  19.         if s.isEmpty():
  20.             a.push(i)
  21.         else:
  22.             temp = s.pop()
  23.             if (i == ')' and temp != '(') or (i == ']' and temp != '[') or (i == '}' and temp != '{'):
  24.                 posible = False
  25.                 break
  26. if posible:
  27.     while not a.isEmpty():
  28.         temp = a.pop()
  29.         if temp == ']':
  30.             temp = '['
  31.         if temp == ')':
  32.             temp = '('
  33.         if temp == '}':
  34.             temp = '{'
  35.         print(temp, end = '')
  36.     print(cin, end = '')
  37.     while not s.isEmpty():
  38.         temp = s.pop()
  39.         if temp == '[':
  40.             temp = ']'
  41.         if temp == '(':
  42.             temp = ')'
  43.         if temp == '{':
  44.             temp = '}'
  45.         print(temp, end = '')
  46. else:
  47.     print('IMPOSSIBLE')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement