Skip to content

Instantly share code, notes, and snippets.

@forestbelton
Created April 6, 2013 03:46
Show Gist options
  • Save forestbelton/5324672 to your computer and use it in GitHub Desktop.
Save forestbelton/5324672 to your computer and use it in GitHub Desktop.
Symmetric group arithmetic
# Computes ab in the symmetric group S_n, where a and b are given in cyclic
# notation.
def compose(a, b, n):
i = 1
out = []
while not i in out:
out.append(i)
i = apply(a, apply(b, i))
if out == [1]:
return []
return out
# Computes a(x) for a in S_n and 1 <= x <= n.
def apply(a, x):
if x in a:
return a[(a.index(x) + 1) % len(a)]
else:
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment