Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def is_balanced(expression):
- stack = []
- for char in expression:
- if char == "(":
- stack.append(char)
- elif char == ")":
- if not stack or stack[-1] != "(":
- return False
- stack.pop()
- return len(stack) == 0
- n = int(input())
- expression = ""
- for _ in range(n):
- line = input().strip()
- expression += line
- result = is_balanced(expression)
- if result:
- print("BALANCED")
- else:
- print("UNBALANCED")
Advertisement
Add Comment
Please, Sign In to add comment