Skip to content

Instantly share code, notes, and snippets.

@octopuscabbage
Last active August 29, 2015 14:01
Show Gist options
  • Save octopuscabbage/0aba7550d5522cbd86d6 to your computer and use it in GitHub Desktop.
Save octopuscabbage/0aba7550d5522cbd86d6 to your computer and use it in GitHub Desktop.
functional parenthesis balancer
def balance(chars):
def testchar(chars, open, closed)
if (open < closed):
return False
if not chars: #chars is empty
return (open == closed)
if (chars.head == '('):
testchar(chars.tail, open+1, closed)
elif (chars.head == ')'):
testchar(chars.tail, open, closed+1)
else:
testchar(chars.tail,open,closed)
testchar(chars,0,0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment