Last active
August 29, 2015 14:05
-
-
Save adammenges/42369bbe6f86ec0c9ce5 to your computer and use it in GitHub Desktop.
balancedParens
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 balancedParens(s): | |
stack, opens, closes = [], ['(', '[', '{'], [')', ']', '}'] | |
for c in s: | |
if c in opens: | |
stack.append(c) | |
elif c in closes: | |
try: | |
if opens.index(stack.pop()) != closes.index(c): | |
return False | |
except (ValueError, IndexError): | |
return False | |
return not stack |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment