import itertools
def solve(tab, seed=None):
if seed is None:
solutions = []
else:
solutions = seed
combinations = []
for i in range(1, len(tab)):
combinations.extend(itertools.combinations(tab, i))
for combination in combinations:
print('combination', combination)
# there is an existing solution
for solution in solutions:
if any(solution.startswith(p) for p in itertools.permutations(combination)):
break
else:
# there is no existing solution
candidates = ["".join(x) for x in itertools.permutations(tab) if "".join(x) not in solutions]
for candidate in candidates:
if any(candidate.startswith(p) for p in itertools.permutations(combination)):
solutions.append(candidate)
break
else:
print(combination)
assert False, "should not happen, because..."
print('solutions', solutions)
return solutions
tab = '0123'
rotations = [(tab[i:] + tab[:i]) for i in range(len(tab))]
for x in solve(tab, rotations):
print(x)