Skip to content

Instantly share code, notes, and snippets.

@goutomroy
Created July 13, 2019 22:02
Show Gist options
  • Save goutomroy/8e7cc8288a4e8e84d33f6838798212ad to your computer and use it in GitHub Desktop.
Save goutomroy/8e7cc8288a4e8e84d33f6838798212ad to your computer and use it in GitHub Desktop.
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