Sichanov

brackets

Sep 23rd, 2021 (edited)
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.50 KB | None | 0 0
  1. number_of_lines = int(input())
  2. is_balanced = True
  3. pattern = ''
  4. for each_line in range(number_of_lines):
  5.     command = input()
  6.     if command == ')' and pattern == '':
  7.         is_balanced = False
  8.         break
  9.     else:
  10.         if command in '()':
  11.             pattern += command
  12.     if pattern == '()':
  13.         is_balanced = True
  14.         pattern = ''
  15.     elif pattern == '((':
  16.         is_balanced = False
  17.         break
  18.  
  19. if is_balanced:
  20.     print('BALANCED')
  21. else:
  22.     print('UNBALANCED')
Add Comment
Please, Sign In to add comment