Advertisement
usmiwka80

4. Balanced Brackets

Aug 24th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | Software | 0 0
  1. lines = len(input())
  2. is_balanced = False
  3. brackets = []
  4.  
  5. for index in range(0, lines):
  6.     symbol = input()
  7.     if symbol.isalnum():
  8.         symbol = input()
  9.     if symbol == "(":
  10.         brackets.append(symbol)
  11.     elif symbol == ")":
  12.         brackets.append(symbol)
  13.  
  14. for index in range(len(brackets)):
  15.     if brackets[index] == "(" and index % 2 == 0:
  16.         is_balanced = True
  17.     elif brackets[index] == ")" and index % 2 != 0 and is_balanced:
  18.         is_balanced = True
  19.     else:
  20.         is_balanced = False
  21.         break
  22.  
  23. if is_balanced:
  24.     print("BALANCED")
  25. else:
  26.     print("UNBALANCED")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement