Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys
- # A cycle is represented as a list:
- # cycle representation
- # (1 2 3) [1, 2, 3]
- #
- # A permutation, expressed as a set of disjoint cycles, is represented
- # as a list of lists:
- # permutation representation
- # (1 2 3)(4 5) [[1, 2, 3], [4, 5]]
- def apply_cycle(i, c):
- if i in c:
- i = c[(c.index(i)+1) % len(c)]
- return i
- def apply_perm_cycles(i, x):
- for c in x:
- i = apply_cycle(i, c)
- return i
- def multiply_perms_cycles(n, x, y):
- for i in range(n):
- j = apply_perm_cycles(apply_perm_cycles(i, y), x)
- print(i,j)
- # full perm representation functions
- def to_cycles(p):
- added = {}
- cycles = [];
- for i in p:
- if i in added: continue
- added[i] = 1
- if p[i] == i: continue
- j = p[i]
- c = [i]
- while j != i:
- c.append(j)
- added[j] = 1
- j = p[j]
- cycles.append(c)
- return cycles
- def multiply_perms_full(x, y):
- z = []
- for i in range(len(y)):
- z.append(x[y[i]])
- return z
- def mult_list(pl): # [p1, p2, ...] perms to multiply
- z = list(range(len(pl[0])))
- pc = pl[:]
- pc.reverse()
- for p in pc:
- z = multiply_perms_full(z, p)
- return z
- def inv(p):
- z = list(range(len(p)))
- for i in p:
- z[p[i]] = i
- return z
- def is_identity(p):
- for i in range(len(p)):
- if p[i] != i:
- return False
- return True
- def find_order(p):
- n = 1
- Pn = [i for i in p]
- while not is_identity(Pn):
- Pn = multiply_perms_full(Pn, p)
- n += 1
- return n
- def shuffle_out(x, flip=None):
- n = len(x)//2
- z = []
- for i in range(n):
- z.append(i)
- z.append(n+i)
- if flip is not None:
- z[flip], z[flip+1] = z[flip+1], z[flip]
- return inv(z)
- def shuffle_in(x, flip=None):
- n = len(x)//2
- z = []
- for i in range(n):
- z.append(n+i)
- z.append(i)
- if flip is not None:
- z[flip], z[flip+1] = z[flip+1], z[flip]
- return inv(z)
- pos = 0
- if len(sys.argv) > 1:
- pos = int(sys.argv[1])
- d = list(range(52))
- A = shuffle_out(d)
- B = shuffle_out(d, pos)
- Bp = inv(B)
- oB = find_order(B)
- s = [Bp, A]
- print(to_cycles(mult_list(s)))
- print(oB)
Add Comment
Please, Sign In to add comment