Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def withinOneModN(a, b):
- return ((b-a)%n) in (0, 1, n-1)
- def enumAll(n, l):
- i = len(l)
- if (i == n):
- yield l
- return
- for j in range(n):
- if j in l:
- continue
- if withinOneModN(i, j):
- continue
- if i > 0 and withinOneModN(j, l[-1]):
- continue
- if i == n-1 and withinOneModN(j, l[0]):
- continue
- for ll in enumAll(n, l+[j]):
- yield ll
- n = 1
- while True:
- for l in enumAll(n, []):
- print(n, [j+1 for j in l])
- break # Comment to print all permutations, not just the first for each n
- n += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement