import random import functools def iter_balanced_parens(num_pairs): if num_pairs == 0: yield "" else: for x in reversed(range(num_pairs)): y = num_pairs - x - 1 assert x + y == num_pairs-1 for right in iter_balanced_parens(y): for left in iter_balanced_parens(x): s = "(" + left + ")" + right assert len(s) == 2 * num_pairs yield s failures = 0 for i in range(25): print(f"num_pairs = {i}") last = None for s in iter_balanced_parens(i): if last is None or last < s: print(s) else: print(s, "Lexicographic ordering error!") failures += 1 last = s if failures >= 7: exit()
Standard input is empty
num_pairs = 0 num_pairs = 1 () num_pairs = 2 (()) ()() num_pairs = 3 ((())) (()()) (())() ()(()) ()()() num_pairs = 4 (((()))) ((()())) ((())()) (()(())) (()()()) ((()))() Lexicographic ordering error! (()())() (())(()) (())()() ()((())) ()(()()) ()(())() ()()(()) ()()()() num_pairs = 5 ((((())))) (((()()))) (((())())) ((()(()))) ((()()())) (((()))()) Lexicographic ordering error! ((()())()) ((())(())) ((())()()) (()((()))) (()(()())) (()(())()) (()()(())) (()()()()) (((())))() Lexicographic ordering error! ((()()))() ((())())() (()(()))() (()()())() ((()))(()) Lexicographic ordering error! (()())(()) ((()))()() Lexicographic ordering error! (()())()() (())((())) (())(()()) (())(())() (())()(()) (())()()() ()(((()))) ()((()())) ()((())()) ()(()(())) ()(()()()) ()((()))() Lexicographic ordering error! ()(()())() ()(())(()) ()(())()() ()()((())) ()()(()()) ()()(())() ()()()(()) ()()()()() num_pairs = 6 (((((()))))) ((((()())))) ((((())()))) (((()(())))) (((()()()))) ((((()))())) Lexicographic ordering error! (((()())())) (((())(()))) (((())()())) ((()((())))) ((()(()()))) ((()(())())) ((()()(()))) ((()()()())) ((((())))()) Lexicographic ordering error! (((()()))()) (((())())()) ((()(()))()) ((()()())()) (((()))(())) Lexicographic ordering error! ((()())(())) (((()))()()) Lexicographic ordering error! ((()())()()) ((())((()))) ((())(()())) ((())(())()) ((())()(())) ((())()()()) (()(((())))) (()((()()))) (()((())())) (()(()(()))) (()(()()())) (()((()))()) Lexicographic ordering error! (()(()())()) (()(())(())) (()(())()()) (()()((()))) (()()(()())) (()()(())()) (()()()(())) (()()()()()) ((((()))))() Lexicographic ordering error! (((()())))() (((())()))() ((()(())))() ((()()()))() (((()))())() Lexicographic ordering error! ((()())())() ((())(()))() ((())()())() (()((())))() (()(()()))() (()(())())() (()()(()))() (()()()())() (((())))(()) Lexicographic ordering error! ((()()))(()) ((())())(()) (()(()))(()) (()()())(()) (((())))()() Lexicographic ordering error! ((()()))()() ((())())()() (()(()))()() (()()())()() ((()))((())) Lexicographic ordering error! (()())((())) ((()))(()()) Lexicographic ordering error! (()())(()()) ((()))(())() Lexicographic ordering error! (()())(())() ((()))()(()) Lexicographic ordering error! (()())()(()) ((()))()()() Lexicographic ordering error! (()())()()() (())(((()))) (())((()())) (())((())()) (())(()(())) (())(()()()) (())((()))() Lexicographic ordering error! (())(()())() (())(())(()) (())(())()() (())()((())) (())()(()()) (())()(())() (())()()(()) (())()()()() ()((((())))) ()(((()()))) ()(((())())) ()((()(()))) ()((()()())) ()(((()))()) Lexicographic ordering error! ()((()())()) ()((())(())) ()((())()()) ()(()((()))) ()(()(()())) ()(()(())()) ()(()()(())) ()(()()()()) ()(((())))() Lexicographic ordering error! ()((()()))() ()((())())() ()(()(()))() ()(()()())() ()((()))(()) Lexicographic ordering error! ()(()())(()) ()((()))()() Lexicographic ordering error! ()(()())()() ()(())((())) ()(())(()()) ()(())(())() ()(())()(()) ()(())()()() ()()(((()))) ()()((()())) ()()((())()) ()()(()(())) ()()(()()()) ()()((()))() Lexicographic ordering error! ()()(()())() ()()(())(()) ()()(())()() ()()()((())) ()()()(()()) ()()()(())() ()()()()(()) ()()()()()()