Blog: 2026-07-12

From razwiki
Jump to navigation Jump to search

Making distinct pairs out of a group of 15

In [33]: def permute(l, n):
    ...:     copy = list(l)
    ...:     new = []
    ...:     index = 0
    ...:     for _ in range(len(l)):
    ...:         new.append(copy.pop(index))
    ...:         index = (index + n) % max(len(copy), 1)
    ...:     return new
    ...: 

In [34]: permute(ns, 1)
Out[34]: [1, 3, 5, 7, 9, 11, 13, 15, 4, 8, 12, 2, 10, 6, 14]

In [35]: permute(ns, 0)
Out[35]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

In [36]: permute(ns, 2)
Out[36]: [1, 4, 7, 10, 13, 2, 6, 11, 15, 8, 14, 9, 5, 12, 3]

In [37]: permute(ns, 3)
Out[37]: [1, 5, 9, 13, 3, 8, 14, 6, 12, 7, 2, 15, 4, 11, 10]