Created
July 11, 2017 21:26
-
-
Save rhlsthrm/8f4eb148b2bfefc5c7d3f2729f33a0d5 to your computer and use it in GitHub Desktop.
Validates that brackets are properly aligned
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
def bracket_validator(arg): | |
openers = [] | |
for char in arg: | |
if char == '(': | |
openers.append('paren') | |
elif char == '{': | |
openers.append('curly') | |
elif char == '[': | |
openers.append('square') | |
elif char == ')': | |
if openers[-1] != 'paren': | |
return False | |
else: | |
del openers[-1] | |
elif char == '}': | |
if openers[-1] != 'curly': | |
return False | |
else: | |
del openers[-1] | |
elif char == ']': | |
if openers[-1] != 'square': | |
return False | |
else: | |
del openers[-1] | |
print "Running with " + arg | |
return True | |
# run your function through some test cases here | |
# remember: debugging is half the battle! | |
print bracket_validator('test input') | |
print bracket_validator('{ [ ] ( ) }') | |
print bracket_validator('{ [ ( ] ) }') | |
print bracket_validator('{ [ }') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment