Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- number_of_lines = int(input())
- string_of_brackets = ""
- # read the input and append only brackets to a string
- for line in range(number_of_lines):
- current_input = input()
- if current_input == ")" or current_input == "(":
- string_of_brackets += current_input
- # First check if the number of entered brackets is even. If not, print string is UNBALANCED and exit
- if len(string_of_brackets) % 2 != 0:
- print("UNBALANCED")
- exit()
- # There are no nested brackets by definition.
- # Check if odd positions are opening brackets. If any of them is not, print UNBALANCED and exit
- for opening_bracket in string_of_brackets[::2]:
- if opening_bracket != "(":
- print("UNBALANCED")
- exit()
- # Check if even positions are closing brackets. If any of them is not, print UNBALANCED and exit
- for closing_bracket in string_of_brackets[1::2]:
- if closing_bracket != ")":
- print("UNBALANCED")
- exit()
- # if all conditions passed, the string is balanced
- print("BALANCED")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement