Created
April 20, 2020 01:18
-
-
Save eliykat/f1cb87ccc0e74aaa66feba9e493ac0a6 to your computer and use it in GitHub Desktop.
Cassidoo 13/4 - generateParens
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
# Entry function | |
def generateParens(n): | |
return parensTree("", n, n) | |
# Recursive function | |
def parensTree(parens, openParens, closeParens): | |
# Left branch recursion | |
if openParens: | |
leftBranch = parensTree(parens + "(", openParens - 1, closeParens) | |
else: | |
leftBranch = [] | |
# Right branch recursion | |
if closeParens and openParens == closeParens: | |
rightBranch = [] | |
elif closeParens: | |
rightBranch = parensTree(parens + ")", openParens, closeParens - 1) | |
else: | |
rightBranch = [parens] | |
return leftBranch + rightBranch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment