-
-
Save sudarshang/84bb069a9479545afb15502564e46507 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
"Generate all strings containing just ( and ) with parens balenced." | |
from itertools import count | |
def parens(): | |
def nested(x): | |
return ("(" * x) + (")" * x) if x > 0 else "" | |
def n_parens(n): | |
yield nested(n) | |
yield from (nested(i) + p for i in range(1, n) for p in n_parens(n - i)) | |
return (x for n in count() for x in n_parens(n)) | |
if __name__ == "__main__": | |
for i, p in enumerate(parens()): | |
print(p) | |
if i > 100: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment