Paste #p8euxe837

1. import itertools
2.
3.
4. def solve(tab, seed=None):
5.     if seed is None:
6.         solutions = []
7.     else:
8.         solutions = seed
9.
10.     combinations = []
11.     for i in range(1, len(tab)):
12.         combinations.extend(itertools.combinations(tab, i))
13.
14.     for combination in combinations:
15.         print('combination', combination)
16.         # there is an existing solution
17.         for solution in solutions:
18.             if any(solution.startswith(p) for p in itertools.permutations(combination)):
19.                 break
20.         else:
21.             # there is no existing solution
22.             candidates = ["".join(x) for x in itertools.permutations(tab) if "".join(x) not in solutions]
23.             for candidate in candidates:
24.                 if any(candidate.startswith(p) for p in itertools.permutations(combination)):
25.                     solutions.append(candidate)
26.                     break
27.             else:
28.                 print(combination)
29.                 assert False, "should not happen, because..."
30.         print('solutions', solutions)
31.     return solutions
32.
33.
34. tab = '0123'
35. rotations = [(tab[i:] + tab[:i]) for i in range(len(tab))]
36. for x in solve(tab, rotations):
37.     print(x)