import itertools
def pk(*args):
print(*args)
return args[-1]
def solve(tab, seed=None):
if seed is None:
solutions = []
else:
solutions = seed
combinations = []
for i in range(1, len(tab) + 1):
combinations.extend(''.join(x) for x in itertools.combinations(tab, i))
assert len(combinations) == 2**len(tab) - 1
px = ["".join(x) for x in itertools.permutations(tab) if "".join(x)]
for combination in combinations:
pcx = [''.join(x) for x in itertools.permutations(combination)]
# check for existing solution
for solution in solutions:
if any(solution.startswith(p) for p in pcx):
# yeah, there is an existing solution
break
else:
# there is no existing solution that is "permutation prefix" of the combination
candidates = [p for p in px if p not in solutions]
for candidate in candidates:
if any(candidate.startswith(p) for p in pcx):
# candidate is solution
solutions.append(candidate)
break
else:
assert False, "should not happen, because..."
return solutions
tab = '01234567'
rotations = [(tab[i:] + tab[:i]) for i in range(len(tab))]
out = solve(tab, rotations)
print(len(out))
for x in out:
print(x)