Created
November 3, 2022 04:35
-
-
Save mbergal/be2803103d64158db62e7885a4c0aa6c 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
from typing import Iterable, List | |
def generate_split(places: int, max: int) -> Iterable[List[int]]: | |
if places == 0: | |
yield [] | |
elif places == 1: | |
yield [max] | |
else: | |
for i in range(0, max + 1): | |
for j in generate_split(places - 1, max - i): | |
yield [i] + j | |
def head_to_parens(head: int) -> Iterable[str]: | |
for i in range(head): | |
for a in generate_split(i + 1, head - i - 1): | |
for s in to_parens(a[0], a[1:]): | |
yield "(" + s + ")" | |
def tail_to_parens(splits: List[int]) -> Iterable[str]: | |
if len(splits) > 0: | |
for parens in to_parens(splits[0], splits[1:]): | |
yield parens | |
else: | |
yield "" | |
def to_parens(head: int, tail_splits: List[int]) -> Iterable[str]: | |
for i in ["()"] if head == 0 else head_to_parens(head): | |
for j in tail_to_parens(tail_splits): | |
yield i + j | |
for r in to_parens(6, []): | |
pass | |
print(r[1:-1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment