Created
April 17, 2017 09:46
-
-
Save DeviaVir/bf88c503f320bff89042cb4236d6ddc4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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