Advertisement
Sichanov

brackets

Sep 23rd, 2021
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. n_lines = int(input())
  2. open_brack = None
  3. closed_brack = None
  4. is_balanced = None
  5.  
  6. counter = 0
  7. for each_line in range(n_lines):
  8.     command = input()
  9.     if command != '(' and command != ')':
  10.         pass
  11.     else:
  12.         if command == '(':
  13.             counter += 1
  14.             if open_brack is None:
  15.                 open_brack = command
  16.             else:
  17.                 closed_brack = command
  18.  
  19.         elif command == ')':
  20.             counter += 1
  21.             if open_brack is None:
  22.                 open_brack = command
  23.             else:
  24.                 closed_brack = command
  25.  
  26.         if counter == 2:
  27.             if open_brack == '(' and closed_brack == ')':
  28.                 is_balanced = True
  29.             else:
  30.                 is_balanced = False
  31.             open_brack = None
  32.             closed_brack = None
  33.             counter = 0
  34.  
  35. if is_balanced:
  36.     print('BALANCED')
  37. else:
  38.     print('UNBALANCED')
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement