Skip to content

Instantly share code, notes, and snippets.

@adammenges
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adammenges/42369bbe6f86ec0c9ce5 to your computer and use it in GitHub Desktop.
Save adammenges/42369bbe6f86ec0c9ce5 to your computer and use it in GitHub Desktop.
balancedParens
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