Skip to content

Instantly share code, notes, and snippets.

@DeviaVir
Created April 17, 2017 09:46
Show Gist options
  • Save DeviaVir/bf88c503f320bff89042cb4236d6ddc4 to your computer and use it in GitHub Desktop.
Save DeviaVir/bf88c503f320bff89042cb4236d6ddc4 to your computer and use it in GitHub Desktop.
open_symbols = ['(', '{', '[']
close_symbols = [')', '}', ']']
def main():
symbols = raw_input()
opened_once = False
open_list = []
close_list = []
last_open = False
for symbol in list(symbols):
# comment out this if, if you want to allow multiple open/close delimiters from the start
if opened_once is True and not len(open_list):
return False
if symbol not in open_symbols and symbol not in close_symbols:
return False
if symbol in open_symbols:
opened_once = True
open_list.append(symbol)
last_open = symbol
elif symbol in close_symbols:
close_list.append(symbol)
open_match = close_symbols.index(symbol)
if last_open == open_symbols[open_match]:
open_list.pop(len(open_list)-1)
close_list.pop(len(close_list)-1)
if len(open_list):
last_open = open_list[-1]
if len(open_list) > 0 or len(close_list) > 0:
return False
return True
print(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment