Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Stack:
- def __init__(self):
- self.items = []
- def isEmpty(self):
- return self.items == []
- def push(self, item):
- self.items.append(item)
- def pop(self):
- return self.items.pop()
- posible = True
- cin = input()
- a=Stack()
- s=Stack()
- for i in cin:
- if ( i == '(' or i == '[' or i == '{' ):
- s.push( i )
- if ( i == ')' or i == ']' or i == '}'):
- if s.isEmpty():
- a.push(i)
- else:
- temp = s.pop()
- if (i == ')' and temp != '(') or (i == ']' and temp != '[') or (i == '}' and temp != '{'):
- posible = False
- break
- if posible:
- while not a.isEmpty():
- temp = a.pop()
- if temp == ']':
- temp = '['
- if temp == ')':
- temp = '('
- if temp == '}':
- temp = '{'
- print(temp, end = '')
- print(cin, end = '')
- while not s.isEmpty():
- temp = s.pop()
- if temp == '[':
- temp = ']'
- if temp == '(':
- temp = ')'
- if temp == '{':
- temp = '}'
- print(temp, end = '')
- else:
- print('IMPOSSIBLE')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement