Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. def withinOneModN(a, b):
  2.     return ((b-a)%n) in (0, 1, n-1)
  3.  
  4. def enumAll(n, l):
  5.     i = len(l)
  6.     if (i == n):
  7.         yield l
  8.         return
  9.     for j in range(n):
  10.         if j in l:
  11.             continue
  12.         if withinOneModN(i, j):
  13.             continue
  14.         if i > 0 and withinOneModN(j, l[-1]):
  15.             continue
  16.         if i == n-1 and withinOneModN(j, l[0]):
  17.             continue
  18.         for ll in enumAll(n, l+[j]):
  19.             yield ll
  20.  
  21. n = 1
  22. while True:
  23.     for l in enumAll(n, []):
  24.         print(n, [j+1 for j in l])
  25.         break # Comment to print all permutations, not just the first for each n
  26.     n += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement