Last active
August 29, 2015 14:01
-
-
Save octopuscabbage/0aba7550d5522cbd86d6 to your computer and use it in GitHub Desktop.
functional parenthesis balancer
This file contains 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 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