Created
July 13, 2019 22:02
-
-
Save goutomroy/8e7cc8288a4e8e84d33f6838798212ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def check_balance(expression): | |
open_list = ['(', '[', '{'] | |
close_list = [')', ']', '}'] | |
stack = [] | |
for c in expression: | |
if c in open_list: | |
stack.append(c) | |
elif c in close_list: | |
pos = close_list.index(c) | |
if len(stack) > 0 and stack[-1] == open_list[pos]: | |
stack.pop() | |
else: | |
return False | |
return True if not stack else False | |
n = int(input()) | |
for i in range(n): | |
exp = input() | |
print(check_balance(exp)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment