The most appealing solution so far seems to be a spin on Python's own permutations
function (source) that can be slightly simplified for this use case; thanks @Peilonrayz:
def combinations(r: int, n: int=10) -> Iterable[Sequence[int]]:
pool = range(n)
indices = list(pool)
cycles = list(range(n, n-r, -1))
r_range = range(r-1, -1, -1)
yield pool[:r]
while True:
for i in r_range:
cycles[i] -= 1
if cycles[i] > 0:
j = cycles[i]
indices[i], indices[-j] = indices[-j], indices[i]
yield indices[:r]
break
indices[i:] = indices[i+1:] + indices[i:i+1]
cycles[i] = n - i
else:
return
As best as I can tell, the way this works:
- This is an iterative algorithm that swaps elements in a mutable list
indices
of length n
from which r
elements will be chosen; these elements will always be chosen from the beginning of the list
indices
starts with a list of elements to be chosen in their original order
- A secondary mutable list
cycles
of length r
controls which elements are swapped
- On an inner condition based on
cycles
, only two elements are exchanged with each other and one output tuple is produced. If that condition is false, an output tuple is not produced, and instead indices
sees a portion of its elements shifted left by one position, with the middle element being bumped to the end.
I don't think transcribing the code above into pseudocode has much value, so instead I'll output a dump that illustrates the value of these control variables over a few iterations where n=10, r=3:
indices=[0, 1, 3, 2, 4, 5, 6, 7, 8, 9] cycles=[10, 9, 7] i=2, indices 2 and 3 were exchanged, output [0, 1, 3]
indices=[0, 1, 4, 2, 3, 5, 6, 7, 8, 9] cycles=[10, 9, 6] i=2, indices 2 and 4 were exchanged, output [0, 1, 4]
indices=[0, 1, 5, 2, 3, 4, 6, 7, 8, 9] cycles=[10, 9, 5] i=2, indices 2 and 5 were exchanged, output [0, 1, 5]
indices=[0, 1, 6, 2, 3, 4, 5, 7, 8, 9] cycles=[10, 9, 4] i=2, indices 2 and 6 were exchanged, output [0, 1, 6]
indices=[0, 1, 7, 2, 3, 4, 5, 6, 8, 9] cycles=[10, 9, 3] i=2, indices 2 and 7 were exchanged, output [0, 1, 7]
indices=[0, 1, 8, 2, 3, 4, 5, 6, 7, 9] cycles=[10, 9, 2] i=2, indices 2 and 8 were exchanged, output [0, 1, 8]
indices=[0, 1, 9, 2, 3, 4, 5, 6, 7, 8] cycles=[10, 9, 1] i=2, indices 2 and 9 were exchanged, output [0, 1, 9]
indices=[0, 1, 9, 2, 3, 4, 5, 6, 7, 8], shifting at 2:
[ \2, 3, 4, 5, 6, 7, 8,/9]
indices=[0, 2, 1, 3, 4, 5, 6, 7, 8, 9] cycles=[10, 8, 8] i=1, indices 1 and 2 were exchanged, output [0, 2, 1]
indices=[0, 2, 3, 1, 4, 5, 6, 7, 8, 9] cycles=[10, 8, 7] i=2, indices 2 and 3 were exchanged, output [0, 2, 3]
indices=[0, 2, 4, 1, 3, 5, 6, 7, 8, 9] cycles=[10, 8, 6] i=2, indices 2 and 4 were exchanged, output [0, 2, 4]
indices=[0, 2, 5, 1, 3, 4, 6, 7, 8, 9] cycles=[10, 8, 5] i=2, indices 2 and 5 were exchanged, output [0, 2, 5]
indices=[0, 2, 6, 1, 3, 4, 5, 7, 8, 9] cycles=[10, 8, 4] i=2, indices 2 and 6 were exchanged, output [0, 2, 6]
indices=[0, 2, 7, 1, 3, 4, 5, 6, 8, 9] cycles=[10, 8, 3] i=2, indices 2 and 7 were exchanged, output [0, 2, 7]
indices=[0, 2, 8, 1, 3, 4, 5, 6, 7, 9] cycles=[10, 8, 2] i=2, indices 2 and 8 were exchanged, output [0, 2, 8]
indices=[0, 2, 9, 1, 3, 4, 5, 6, 7, 8] cycles=[10, 8, 1] i=2, indices 2 and 9 were exchanged, output [0, 2, 9]
indices=[0, 2, 9, 1, 3, 4, 5, 6, 7, 8], shifting at 2:
[ \1, 3, 4, 5, 6, 7, 8,/9]
indices=[0, 3, 1, 2, 4, 5, 6, 7, 8, 9] cycles=[10, 7, 8] i=1, indices 1 and 3 were exchanged, output [0, 3, 1]
indices=[0, 3, 2, 1, 4, 5, 6, 7, 8, 9] cycles=[10, 7, 7] i=2, indices 2 and 3 were exchanged, output [0, 3, 2]
indices=[0, 3, 4, 1, 2, 5, 6, 7, 8, 9] cycles=[10, 7, 6] i=2, indices 2 and 4 were exchanged, output [0, 3, 4]
indices=[0, 3, 5, 1, 2, 4, 6, 7, 8, 9] cycles=[10, 7, 5] i=2, indices 2 and 5 were exchanged, output [0, 3, 5]
indices=[0, 3, 6, 1, 2, 4, 5, 7, 8, 9] cycles=[10, 7, 4] i=2, indices 2 and 6 were exchanged, output [0, 3, 6]
indices=[0, 3, 7, 1, 2, 4, 5, 6, 8, 9] cycles=[10, 7, 3] i=2, indices 2 and 7 were exchanged, output [0, 3, 7]
indices=[0, 3, 8, 1, 2, 4, 5, 6, 7, 9] cycles=[10, 7, 2] i=2, indices 2 and 8 were exchanged, output [0, 3, 8]
indices=[0, 3, 9, 1, 2, 4, 5, 6, 7, 8] cycles=[10, 7, 1] i=2, indices 2 and 9 were exchanged, output [0, 3, 9]
indices=[0, 3, 9, 1, 2, 4, 5, 6, 7, 8], shifting at 2:
[ \1, 2, 4, 5, 6, 7, 8,/9]
indices=[0, 4, 1, 2, 3, 5, 6, 7, 8, 9] cycles=[10, 6, 8] i=1, indices 1 and 4 were exchanged, output [0, 4, 1]
indices=[0, 4, 2, 1, 3, 5, 6, 7, 8, 9] cycles=[10, 6, 7] i=2, indices 2 and 3 were exchanged, output [0, 4, 2]
indices=[0, 4, 3, 1, 2, 5, 6, 7, 8, 9] cycles=[10, 6, 6] i=2, indices 2 and 4 were exchanged, output [0, 4, 3]
indices=[0, 4, 5, 1, 2, 3, 6, 7, 8, 9] cycles=[10, 6, 5] i=2, indices 2 and 5 were exchanged, output [0, 4, 5]
indices=[0, 4, 6, 1, 2, 3, 5, 7, 8, 9] cycles=[10, 6, 4] i=2, indices 2 and 6 were exchanged, output [0, 4, 6]
indices=[0, 4, 7, 1, 2, 3, 5, 6, 8, 9] cycles=[10, 6, 3] i=2, indices 2 and 7 were exchanged, output [0, 4, 7]
indices=[0, 4, 8, 1, 2, 3, 5, 6, 7, 9] cycles=[10, 6, 2] i=2, indices 2 and 8 were exchanged, output [0, 4, 8]
indices=[0, 4, 9, 1, 2, 3, 5, 6, 7, 8] cycles=[10, 6, 1] i=2, indices 2 and 9 were exchanged, output [0, 4, 9]
indices=[0, 4, 9, 1, 2, 3, 5, 6, 7, 8], shifting at 2:
[ \1, 2, 3, 5, 6, 7, 8,/9]
indices=[0, 5, 1, 2, 3, 4, 6, 7, 8, 9] cycles=[10, 5, 8] i=1, indices 1 and 5 were exchanged, output [0, 5, 1]
indices=[0, 5, 2, 1, 3, 4, 6, 7, 8, 9] cycles=[10, 5, 7] i=2, indices 2 and 3 were exchanged, output [0, 5, 2]
indices=[0, 5, 3, 1, 2, 4, 6, 7, 8, 9] cycles=[10, 5, 6] i=2, indices 2 and 4 were exchanged, output [0, 5, 3]
indices=[0, 5, 4, 1, 2, 3, 6, 7, 8, 9] cycles=[10, 5, 5] i=2, indices 2 and 5 were exchanged, output [0, 5, 4]
indices=[0, 5, 6, 1, 2, 3, 4, 7, 8, 9] cycles=[10, 5, 4] i=2, indices 2 and 6 were exchanged, output [0, 5, 6]
indices=[0, 5, 7, 1, 2, 3, 4, 6, 8, 9] cycles=[10, 5, 3] i=2, indices 2 and 7 were exchanged, output [0, 5, 7]
indices=[0, 5, 8, 1, 2, 3, 4, 6, 7, 9] cycles=[10, 5, 2] i=2, indices 2 and 8 were exchanged, output [0, 5, 8]
indices=[0, 5, 9, 1, 2, 3, 4, 6, 7, 8] cycles=[10, 5, 1] i=2, indices 2 and 9 were exchanged, output [0, 5, 9]
indices=[0, 5, 9, 1, 2, 3, 4, 6, 7, 8], shifting at 2:
[ \1, 2, 3, 4, 6, 7, 8,/9]
indices=[0, 6, 1, 2, 3, 4, 5, 7, 8, 9] cycles=[10, 4, 8] i=1, indices 1 and 6 were exchanged, output [0, 6, 1]
indices=[0, 6, 2, 1, 3, 4, 5, 7, 8, 9] cycles=[10, 4, 7] i=2, indices 2 and 3 were exchanged, output [0, 6, 2]
indices=[0, 6, 3, 1, 2, 4, 5, 7, 8, 9] cycles=[10, 4, 6] i=2, indices 2 and 4 were exchanged, output [0, 6, 3]
indices=[0, 6, 4, 1, 2, 3, 5, 7, 8, 9] cycles=[10, 4, 5] i=2, indices 2 and 5 were exchanged, output [0, 6, 4]
indices=[0, 6, 5, 1, 2, 3, 4, 7, 8, 9] cycles=[10, 4, 4] i=2, indices 2 and 6 were exchanged, output [0, 6, 5]
indices=[0, 6, 7, 1, 2, 3, 4, 5, 8, 9] cycles=[10, 4, 3] i=2, indices 2 and 7 were exchanged, output [0, 6, 7]
indices=[0, 6, 8, 1, 2, 3, 4, 5, 7, 9] cycles=[10, 4, 2] i=2, indices 2 and 8 were exchanged, output [0, 6, 8]
indices=[0, 6, 9, 1, 2, 3, 4, 5, 7, 8] cycles=[10, 4, 1] i=2, indices 2 and 9 were exchanged, output [0, 6, 9]
indices=[0, 6, 9, 1, 2, 3, 4, 5, 7, 8], shifting at 2:
[ \1, 2, 3, 4, 5, 7, 8,/9]
indices=[0, 7, 1, 2, 3, 4, 5, 6, 8, 9] cycles=[10, 3, 8] i=1, indices 1 and 7 were exchanged, output [0, 7, 1]
indices=[0, 7, 2, 1, 3, 4, 5, 6, 8, 9] cycles=[10, 3, 7] i=2, indices 2 and 3 were exchanged, output [0, 7, 2]
indices=[0, 7, 3, 1, 2, 4, 5, 6, 8, 9] cycles=[10, 3, 6] i=2, indices 2 and 4 were exchanged, output [0, 7, 3]
indices=[0, 7, 4, 1, 2, 3, 5, 6, 8, 9] cycles=[10, 3, 5] i=2, indices 2 and 5 were exchanged, output [0, 7, 4]
indices=[0, 7, 5, 1, 2, 3, 4, 6, 8, 9] cycles=[10, 3, 4] i=2, indices 2 and 6 were exchanged, output [0, 7, 5]
indices=[0, 7, 6, 1, 2, 3, 4, 5, 8, 9] cycles=[10, 3, 3] i=2, indices 2 and 7 were exchanged, output [0, 7, 6]
indices=[0, 7, 8, 1, 2, 3, 4, 5, 6, 9] cycles=[10, 3, 2] i=2, indices 2 and 8 were exchanged, output [0, 7, 8]
indices=[0, 7, 9, 1, 2, 3, 4, 5, 6, 8] cycles=[10, 3, 1] i=2, indices 2 and 9 were exchanged, output [0, 7, 9]
indices=[0, 7, 9, 1, 2, 3, 4, 5, 6, 8], shifting at 2:
[ \1, 2, 3, 4, 5, 6, 8,/9]
indices=[0, 8, 1, 2, 3, 4, 5, 6, 7, 9] cycles=[10, 2, 8] i=1, indices 1 and 8 were exchanged, output [0, 8, 1]
indices=[0, 8, 2, 1, 3, 4, 5, 6, 7, 9] cycles=[10, 2, 7] i=2, indices 2 and 3 were exchanged, output [0, 8, 2]
indices=[0, 8, 3, 1, 2, 4, 5, 6, 7, 9] cycles=[10, 2, 6] i=2, indices 2 and 4 were exchanged, output [0, 8, 3]
indices=[0, 8, 4, 1, 2, 3, 5, 6, 7, 9] cycles=[10, 2, 5] i=2, indices 2 and 5 were exchanged, output [0, 8, 4]
indices=[0, 8, 5, 1, 2, 3, 4, 6, 7, 9] cycles=[10, 2, 4] i=2, indices 2 and 6 were exchanged, output [0, 8, 5]
indices=[0, 8, 6, 1, 2, 3, 4, 5, 7, 9] cycles=[10, 2, 3] i=2, indices 2 and 7 were exchanged, output [0, 8, 6]
indices=[0, 8, 7, 1, 2, 3, 4, 5, 6, 9] cycles=[10, 2, 2] i=2, indices 2 and 8 were exchanged, output [0, 8, 7]
indices=[0, 8, 9, 1, 2, 3, 4, 5, 6, 7] cycles=[10, 2, 1] i=2, indices 2 and 9 were exchanged, output [0, 8, 9]
indices=[0, 8, 9, 1, 2, 3, 4, 5, 6, 7], shifting at 2:
[ \1, 2, 3, 4, 5, 6, 7,/9]
indices=[0, 9, 1, 2, 3, 4, 5, 6, 7, 8] cycles=[10, 1, 8] i=1, indices 1 and 9 were exchanged, output [0, 9, 1]
indices=[0, 9, 2, 1, 3, 4, 5, 6, 7, 8] cycles=[10, 1, 7] i=2, indices 2 and 3 were exchanged, output [0, 9, 2]
indices=[0, 9, 3, 1, 2, 4, 5, 6, 7, 8] cycles=[10, 1, 6] i=2, indices 2 and 4 were exchanged, output [0, 9, 3]
indices=[0, 9, 4, 1, 2, 3, 5, 6, 7, 8] cycles=[10, 1, 5] i=2, indices 2 and 5 were exchanged, output [0, 9, 4]
indices=[0, 9, 5, 1, 2, 3, 4, 6, 7, 8] cycles=[10, 1, 4] i=2, indices 2 and 6 were exchanged, output [0, 9, 5]
indices=[0, 9, 6, 1, 2, 3, 4, 5, 7, 8] cycles=[10, 1, 3] i=2, indices 2 and 7 were exchanged, output [0, 9, 6]
indices=[0, 9, 7, 1, 2, 3, 4, 5, 6, 8] cycles=[10, 1, 2] i=2, indices 2 and 8 were exchanged, output [0, 9, 7]
indices=[0, 9, 8, 1, 2, 3, 4, 5, 6, 7] cycles=[10, 1, 1] i=2, indices 2 and 9 were exchanged, output [0, 9, 8]
indices=[0, 9, 8, 1, 2, 3, 4, 5, 6, 7], shifting at 2:
[ \1, 2, 3, 4, 5, 6, 7,/8]
indices=[0, 9, 1, 2, 3, 4, 5, 6, 7, 8], shifting at 1:
[ \1, 2, 3, 4, 5, 6, 7, 8,/9]
indices=[1, 0, 2, 3, 4, 5, 6, 7, 8, 9] cycles=[9, 9, 8] i=0, indices 0 and 1 were exchanged, output [1, 0, 2]
indices=[1, 0, 3, 2, 4, 5, 6, 7, 8, 9] cycles=[9, 9, 7] i=2, indices 2 and 3 were exchanged, output [1, 0, 3]
indices=[1, 0, 4, 2, 3, 5, 6, 7, 8, 9] cycles=[9, 9, 6] i=2, indices 2 and 4 were exchanged, output [1, 0, 4]
indices=[1, 0, 5, 2, 3, 4, 6, 7, 8, 9] cycles=[9, 9, 5] i=2, indices 2 and 5 were exchanged, output [1, 0, 5]
indices=[1, 0, 6, 2, 3, 4, 5, 7, 8, 9] cycles=[9, 9, 4] i=2, indices 2 and 6 were exchanged, output [1, 0, 6]
indices=[1, 0, 7, 2, 3, 4, 5, 6, 8, 9] cycles=[9, 9, 3] i=2, indices 2 and 7 were exchanged, output [1, 0, 7]
indices=[1, 0, 8, 2, 3, 4, 5, 6, 7, 9] cycles=[9, 9, 2] i=2, indices 2 and 8 were exchanged, output [1, 0, 8]
indices=[1, 0, 9, 2, 3, 4, 5, 6, 7, 8] cycles=[9, 9, 1] i=2, indices 2 and 9 were exchanged, output [1, 0, 9]
indices=[1, 0, 9, 2, 3, 4, 5, 6, 7, 8], shifting at 2:
[ \2, 3, 4, 5, 6, 7, 8,/9]
indices=[1, 2, 0, 3, 4, 5, 6, 7, 8, 9] cycles=[9, 8, 8] i=1, indices 1 and 2 were exchanged, output [1, 2, 0]
indices=[1, 2, 3, 0, 4, 5, 6, 7, 8, 9] cycles=[9, 8, 7] i=2, indices 2 and 3 were exchanged, output [1, 2, 3]
indices=[1, 2, 4, 0, 3, 5, 6, 7, 8, 9] cycles=[9, 8, 6] i=2, indices 2 and 4 were exchanged, output [1, 2, 4]
indices=[1, 2, 5, 0, 3, 4, 6, 7, 8, 9] cycles=[9, 8, 5] i=2, indices 2 and 5 were exchanged, output [1, 2, 5]
indices=[1, 2, 6, 0, 3, 4, 5, 7, 8, 9] cycles=[9, 8, 4] i=2, indices 2 and 6 were exchanged, output [1, 2, 6]
indices=[1, 2, 7, 0, 3, 4, 5, 6, 8, 9] cycles=[9, 8, 3] i=2, indices 2 and 7 were exchanged, output [1, 2, 7]
indices=[1, 2, 8, 0, 3, 4, 5, 6, 7, 9] cycles=[9, 8, 2] i=2, indices 2 and 8 were exchanged, output [1, 2, 8]
indices=[1, 2, 9, 0, 3, 4, 5, 6, 7, 8] cycles=[9, 8, 1] i=2, indices 2 and 9 were exchanged, output [1, 2, 9]
indices=[1, 2, 9, 0, 3, 4, 5, 6, 7, 8], shifting at 2:
[ \0, 3, 4, 5, 6, 7, 8,/9]
indices=[1, 3, 0, 2, 4, 5, 6, 7, 8, 9] cycles=[9, 7, 8] i=1, indices 1 and 3 were exchanged, output [1, 3, 0]
indices=[1, 3, 2, 0, 4, 5, 6, 7, 8, 9] cycles=[9, 7, 7] i=2, indices 2 and 3 were exchanged, output [1, 3, 2]
indices=[1, 3, 4, 0, 2, 5, 6, 7, 8, 9] cycles=[9, 7, 6] i=2, indices 2 and 4 were exchanged, output [1, 3, 4]
indices=[1, 3, 5, 0, 2, 4, 6, 7, 8, 9] cycles=[9, 7, 5] i=2, indices 2 and 5 were exchanged, output [1, 3, 5]
indices=[1, 3, 6, 0, 2, 4, 5, 7, 8, 9] cycles=[9, 7, 4] i=2, indices 2 and 6 were exchanged, output [1, 3, 6]
indices=[1, 3, 7, 0, 2, 4, 5, 6, 8, 9] cycles=[9, 7, 3] i=2, indices 2 and 7 were exchanged, output [1, 3, 7]
indices=[1, 3, 8, 0, 2, 4, 5, 6, 7, 9] cycles=[9, 7, 2] i=2, indices 2 and 8 were exchanged, output [1, 3, 8]
indices=[1, 3, 9, 0, 2, 4, 5, 6, 7, 8] cycles=[9, 7, 1] i=2, indices 2 and 9 were exchanged, output [1, 3, 9]
indices=[1, 3, 9, 0, 2, 4, 5, 6, 7, 8], shifting at 2:
[ \0, 2, 4, 5, 6, 7, 8,/9]
indices=[1, 4, 0, 2, 3, 5, 6, 7, 8, 9] cycles=[9, 6, 8] i=1, indices 1 and 4 were exchanged, output [1, 4, 0]
indices=[1, 4, 2, 0, 3, 5, 6, 7, 8, 9] cycles=[9, 6, 7] i=2, indices 2 and 3 were exchanged, output [1, 4, 2]
indices=[1, 4, 3, 0, 2, 5, 6, 7, 8, 9] cycles=[9, 6, 6] i=2, indices 2 and 4 were exchanged, output [1, 4, 3]
indices=[1, 4, 5, 0, 2, 3, 6, 7, 8, 9] cycles=[9, 6, 5] i=2, indices 2 and 5 were exchanged, output [1, 4, 5]
indices=[1, 4, 6, 0, 2, 3, 5, 7, 8, 9] cycles=[9, 6, 4] i=2, indices 2 and 6 were exchanged, output [1, 4, 6]
indices=[1, 4, 7, 0, 2, 3, 5, 6, 8, 9] cycles=[9, 6, 3] i=2, indices 2 and 7 were exchanged, output [1, 4, 7]
indices=[1, 4, 8, 0, 2, 3, 5, 6, 7, 9] cycles=[9, 6, 2] i=2, indices 2 and 8 were exchanged, output [1, 4, 8]
indices=[1, 4, 9, 0, 2, 3, 5, 6, 7, 8] cycles=[9, 6, 1] i=2, indices 2 and 9 were exchanged, output [1, 4, 9]
indices=[1, 4, 9, 0, 2, 3, 5, 6, 7, 8], shifting at 2:
[ \0, 2, 3, 5, 6, 7, 8,/9]
indices=[1, 5, 0, 2, 3, 4, 6, 7, 8, 9] cycles=[9, 5, 8] i=1, indices 1 and 5 were exchanged, output [1, 5, 0]
indices=[1, 5, 2, 0, 3, 4, 6, 7, 8, 9] cycles=[9, 5, 7] i=2, indices 2 and 3 were exchanged, output [1, 5, 2]
indices=[1, 5, 3, 0, 2, 4, 6, 7, 8, 9] cycles=[9, 5, 6] i=2, indices 2 and 4 were exchanged, output [1, 5, 3]
indices=[1, 5, 4, 0, 2, 3, 6, 7, 8, 9] cycles=[9, 5, 5] i=2, indices 2 and 5 were exchanged, output [1, 5, 4]
indices=[1, 5, 6, 0, 2, 3, 4, 7, 8, 9] cycles=[9, 5, 4] i=2, indices 2 and 6 were exchanged, output [1, 5, 6]
indices=[1, 5, 7, 0, 2, 3, 4, 6, 8, 9] cycles=[9, 5, 3] i=2, indices 2 and 7 were exchanged, output [1, 5, 7]
indices=[1, 5, 8, 0, 2, 3, 4, 6, 7, 9] cycles=[9, 5, 2] i=2, indices 2 and 8 were exchanged, output [1, 5, 8]
indices=[1, 5, 9, 0, 2, 3, 4, 6, 7, 8] cycles=[9, 5, 1] i=2, indices 2 and 9 were exchanged, output [1, 5, 9]
indices=[1, 5, 9, 0, 2, 3, 4, 6, 7, 8], shifting at 2:
[ \0, 2, 3, 4, 6, 7, 8,/9]
indices=[1, 6, 0, 2, 3, 4, 5, 7, 8, 9] cycles=[9, 4, 8] i=1, indices 1 and 6 were exchanged, output [1, 6, 0]
indices=[1, 6, 2, 0, 3, 4, 5, 7, 8, 9] cycles=[9, 4, 7] i=2, indices 2 and 3 were exchanged, output [1, 6, 2]
indices=[1, 6, 3, 0, 2, 4, 5, 7, 8, 9] cycles=[9, 4, 6] i=2, indices 2 and 4 were exchanged, output [1, 6, 3]
indices=[1, 6, 4, 0, 2, 3, 5, 7, 8, 9] cycles=[9, 4, 5] i=2, indices 2 and 5 were exchanged, output [1, 6, 4]
indices=[1, 6, 5, 0, 2, 3, 4, 7, 8, 9] cycles=[9, 4, 4] i=2, indices 2 and 6 were exchanged, output [1, 6, 5]
indices=[1, 6, 7, 0, 2, 3, 4, 5, 8, 9] cycles=[9, 4, 3] i=2, indices 2 and 7 were exchanged, output [1, 6, 7]
indices=[1, 6, 8, 0, 2, 3, 4, 5, 7, 9] cycles=[9, 4, 2] i=2, indices 2 and 8 were exchanged, output [1, 6, 8]
indices=[1, 6, 9, 0, 2, 3, 4, 5, 7, 8] cycles=[9, 4, 1] i=2, indices 2 and 9 were exchanged, output [1, 6, 9]
indices=[1, 6, 9, 0, 2, 3, 4, 5, 7, 8], shifting at 2:
[ \0, 2, 3, 4, 5, 7, 8,/9]
indices=[1, 7, 0, 2, 3, 4, 5, 6, 8, 9] cycles=[9, 3, 8] i=1, indices 1 and 7 were exchanged, output [1, 7, 0]
indices=[1, 7, 2, 0, 3, 4, 5, 6, 8, 9] cycles=[9, 3, 7] i=2, indices 2 and 3 were exchanged, output [1, 7, 2]
indices=[1, 7, 3, 0, 2, 4, 5, 6, 8, 9] cycles=[9, 3, 6] i=2, indices 2 and 4 were exchanged, output [1, 7, 3]
indices=[1, 7, 4, 0, 2, 3, 5, 6, 8, 9] cycles=[9, 3, 5] i=2, indices 2 and 5 were exchanged, output [1, 7, 4]
indices=[1, 7, 5, 0, 2, 3, 4, 6, 8, 9] cycles=[9, 3, 4] i=2, indices 2 and 6 were exchanged, output [1, 7, 5]
indices=[1, 7, 6, 0, 2, 3, 4, 5, 8, 9] cycles=[9, 3, 3] i=2, indices 2 and 7 were exchanged, output [1, 7, 6]
indices=[1, 7, 8, 0, 2, 3, 4, 5, 6, 9] cycles=[9, 3, 2] i=2, indices 2 and 8 were exchanged, output [1, 7, 8]
indices=[1, 7, 9, 0, 2, 3, 4, 5, 6, 8] cycles=[9, 3, 1] i=2, indices 2 and 9 were exchanged, output [1, 7, 9]
indices=[1, 7, 9, 0, 2, 3, 4, 5, 6, 8], shifting at 2:
[ \0, 2, 3, 4, 5, 6, 8,/9]
indices=[1, 8, 0, 2, 3, 4, 5, 6, 7, 9] cycles=[9, 2, 8] i=1, indices 1 and 8 were exchanged, output [1, 8, 0]
indices=[1, 8, 2, 0, 3, 4, 5, 6, 7, 9] cycles=[9, 2, 7] i=2, indices 2 and 3 were exchanged, output [1, 8, 2]
indices=[1, 8, 3, 0, 2, 4, 5, 6, 7, 9] cycles=[9, 2, 6] i=2, indices 2 and 4 were exchanged, output [1, 8, 3]
indices=[1, 8, 4, 0, 2, 3, 5, 6, 7, 9] cycles=[9, 2, 5] i=2, indices 2 and 5 were exchanged, output [1, 8, 4]
indices=[1, 8, 5, 0, 2, 3, 4, 6, 7, 9] cycles=[9, 2, 4] i=2, indices 2 and 6 were exchanged, output [1, 8, 5]
indices=[1, 8, 6, 0, 2, 3, 4, 5, 7, 9] cycles=[9, 2, 3] i=2, indices 2 and 7 were exchanged, output [1, 8, 6]
indices=[1, 8, 7, 0, 2, 3, 4, 5, 6, 9] cycles=[9, 2, 2] i=2, indices 2 and 8 were exchanged, output [1, 8, 7]
indices=[1, 8, 9, 0, 2, 3, 4, 5, 6, 7] cycles=[9, 2, 1] i=2, indices 2 and 9 were exchanged, output [1, 8, 9]
indices=[1, 8, 9, 0, 2, 3, 4, 5, 6, 7], shifting at 2:
[ \0, 2, 3, 4, 5, 6, 7,/9]
indices=[1, 9, 0, 2, 3, 4, 5, 6, 7, 8] cycles=[9, 1, 8] i=1, indices 1 and 9 were exchanged, output [1, 9, 0]
indices=[1, 9, 2, 0, 3, 4, 5, 6, 7, 8] cycles=[9, 1, 7] i=2, indices 2 and 3 were exchanged, output [1, 9, 2]
indices=[1, 9, 3, 0, 2, 4, 5, 6, 7, 8] cycles=[9, 1, 6] i=2, indices 2 and 4 were exchanged, output [1, 9, 3]
indices=[1, 9, 4, 0, 2, 3, 5, 6, 7, 8] cycles=[9, 1, 5] i=2, indices 2 and 5 were exchanged, output [1, 9, 4]
indices=[1, 9, 5, 0, 2, 3, 4, 6, 7, 8] cycles=[9, 1, 4] i=2, indices 2 and 6 were exchanged, output [1, 9, 5]
indices=[1, 9, 6, 0, 2, 3, 4, 5, 7, 8] cycles=[9, 1, 3] i=2, indices 2 and 7 were exchanged, output [1, 9, 6]
indices=[1, 9, 7, 0, 2, 3, 4, 5, 6, 8] cycles=[9, 1, 2] i=2, indices 2 and 8 were exchanged, output [1, 9, 7]
indices=[1, 9, 8, 0, 2, 3, 4, 5, 6, 7] cycles=[9, 1, 1] i=2, indices 2 and 9 were exchanged, output [1, 9, 8]
indices=[1, 9, 8, 0, 2, 3, 4, 5, 6, 7], shifting at 2:
[ \0, 2, 3, 4, 5, 6, 7,/8]
indices=[1, 9, 0, 2, 3, 4, 5, 6, 7, 8], shifting at 1:
[ \0, 2, 3, 4, 5, 6, 7, 8,/9]
...
indices=[9, 8, 7, 0, 1, 2, 3, 4, 5, 6] cycles=[1, 1, 1] i=2, indices 2 and 9 were exchanged, output [9, 8, 7]
indices=[9, 8, 7, 0, 1, 2, 3, 4, 5, 6], shifting at 2:
[ \0, 1, 2, 3, 4, 5, 6,/7]
indices=[9, 8, 0, 1, 2, 3, 4, 5, 6, 7], shifting at 1:
[ \0, 1, 2, 3, 4, 5, 6, 7,/8]
indices=[9, 0, 1, 2, 3, 4, 5, 6, 7, 8], shifting at 0:
[ \0, 1, 2, 3, 4, 5, 6, 7, 8,/9]
As to the details on how and why exactly this works - the answer is complicated and requires theoretical understanding of permutation cycles that I frankly don't have.