Guest User

Untitled

a guest
Aug 6th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import sys
  3.  
  4. # A cycle is represented as a list:
  5. #   cycle               representation
  6. #   (1 2 3)             [1, 2, 3]
  7. #
  8. # A permutation, expressed as a set of disjoint cycles, is represented
  9. # as a list of lists:
  10. #   permutation         representation
  11. #   (1 2 3)(4 5)        [[1, 2, 3], [4, 5]]
  12.  
  13. def apply_cycle(i, c):
  14.     if i in c:
  15.         i = c[(c.index(i)+1) % len(c)]
  16.     return i
  17.  
  18. def apply_perm_cycles(i, x):
  19.     for c in x:
  20.         i = apply_cycle(i, c)
  21.     return i
  22.  
  23. def multiply_perms_cycles(n, x, y):
  24.     for i in range(n):
  25.         j = apply_perm_cycles(apply_perm_cycles(i, y), x)
  26.         print(i,j)
  27.  
  28. # full perm representation functions
  29.  
  30. def to_cycles(p):
  31.     added = {}
  32.     cycles = [];
  33.     for i in p:
  34.         if i in added: continue
  35.         added[i] = 1
  36.         if p[i] == i: continue
  37.         j = p[i]
  38.         c = [i]
  39.         while j != i:
  40.             c.append(j)
  41.             added[j] = 1
  42.             j = p[j]
  43.         cycles.append(c)
  44.     return cycles
  45.  
  46. def multiply_perms_full(x, y):
  47.     z = []
  48.     for i in range(len(y)):
  49.         z.append(x[y[i]])
  50.     return z
  51.  
  52. def mult_list(pl):      # [p1, p2, ...] perms to multiply
  53.     z = list(range(len(pl[0])))
  54.     pc = pl[:]
  55.     pc.reverse()
  56.     for p in pc:
  57.         z = multiply_perms_full(z, p)
  58.     return z
  59.  
  60. def inv(p):
  61.     z = list(range(len(p)))
  62.     for i in p:
  63.         z[p[i]] = i
  64.     return z
  65.  
  66. def is_identity(p):
  67.     for i in range(len(p)):
  68.         if p[i] != i:
  69.             return False
  70.     return True
  71.  
  72. def find_order(p):
  73.     n = 1
  74.     Pn = [i for i in p]
  75.     while not is_identity(Pn):
  76.         Pn = multiply_perms_full(Pn, p)
  77.         n += 1
  78.     return n
  79.  
  80. def shuffle_out(x, flip=None):
  81.     n = len(x)//2
  82.     z = []
  83.     for i in range(n):
  84.         z.append(i)
  85.         z.append(n+i)
  86.     if flip is not None:
  87.         z[flip], z[flip+1] = z[flip+1], z[flip]
  88.     return inv(z)
  89.  
  90. def shuffle_in(x, flip=None):
  91.     n = len(x)//2
  92.     z = []
  93.     for i in range(n):
  94.         z.append(n+i)
  95.         z.append(i)
  96.     if flip is not None:
  97.         z[flip], z[flip+1] = z[flip+1], z[flip]
  98.     return inv(z)
  99.  
  100. pos = 0
  101. if len(sys.argv) > 1:
  102.     pos = int(sys.argv[1])
  103.  
  104. d = list(range(52))
  105. A = shuffle_out(d)
  106. B = shuffle_out(d, pos)
  107. Bp = inv(B)
  108. oB = find_order(B)
  109. s = [Bp, A]
  110. print(to_cycles(mult_list(s)))
  111. print(oB)
  112.  
Add Comment
Please, Sign In to add comment