Created
December 17, 2018 19:17
-
-
Save zevaverbach/a12b620b5a9efa0b8859a90e1721daea to your computer and use it in GitHub Desktop.
braces.py
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
MATCHED_LOOKUP = {k: v for k, v in ["()", "[]", "{}"]} | |
def is_valid(string): | |
open_braces = [] | |
for char in string: | |
if char in MATCHED_LOOKUP: | |
open_braces.append(char) | |
else: | |
if not open_braces or MATCHED_LOOKUP[open_braces.pop()] != char: | |
return False | |
return len(open_braces) == 0 | |
for s in ["{}", "{[()]}({})", "{[}]", "{]}", "{[]}", "[]{", "{{}"]: | |
print(s) | |
print(is_valid(s)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment