fork download
  1. import random
  2. import functools
  3.  
  4. def iter_balanced_parens(num_pairs):
  5. if num_pairs == 0:
  6. yield ""
  7. else:
  8. for x in reversed(range(num_pairs)):
  9. y = num_pairs - x - 1
  10. assert x + y == num_pairs-1
  11. for right in iter_balanced_parens(y):
  12. for left in iter_balanced_parens(x):
  13. s = "(" + left + ")" + right
  14. assert len(s) == 2 * num_pairs
  15. yield s
  16.  
  17. failures = 0
  18. for i in range(25):
  19. print(f"num_pairs = {i}")
  20. last = None
  21. for s in iter_balanced_parens(i):
  22. if last is None or last < s:
  23. print(s)
  24. else:
  25. print(s, "Lexicographic ordering error!")
  26. failures += 1
  27. last = s
  28. if failures >= 7:
  29. exit()
Success #stdin #stdout 0.03s 11720KB
stdin
Standard input is empty
stdout
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!
()()(()())()
()()(())(())
()()(())()()
()()()((()))
()()()(()())
()()()(())()
()()()()(())
()()()()()()