Skip to content

Instantly share code, notes, and snippets.

@bluven
Created September 1, 2014 12:28
Show Gist options
  • Save bluven/3225d64e2c2d21cdd576 to your computer and use it in GitHub Desktop.
Save bluven/3225d64e2c2d21cdd576 to your computer and use it in GitHub Desktop.
check if a string is parentheses balanced
def is_parentheses_balanced(expression):
stack = []
for symbol in expression:
if symbol == '(':
stack.append(symbol)
if symbol == ')':
if (not stack) or stack.pop(-1) != '(':
return False
if stack:
return False
return True
print is_parentheses_balanced('((())())()')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment