Skip to content

Instantly share code, notes, and snippets.

@thomastay
Created January 16, 2018 05:28
Show Gist options
  • Save thomastay/87a40313ab3e99fa994a1515b7e3dc3d to your computer and use it in GitHub Desktop.
Save thomastay/87a40313ab3e99fa994a1515b7e3dc3d to your computer and use it in GitHub Desktop.
cycleParser for Math 296
#!python3
from itertools import product
def parseCycle(cycle, n):
permutation = [0 for i in range(n)]
cycle_start = 0
prev = 0
for i in range(len(cycle)):
cur = cycle[i]
if cur == cycle_start:
continue
if cur == "(":
cycle_start = int(cycle[i+1])
prev = cycle_start
continue
if cur == ")":
permutation[prev-1] = int(cycle_start)
cycle_start = 0
prev = 0
continue
permutation[prev-1] = int(cur)
prev = int(cur)
for i in range(n):
if permutation[i] == 0:
permutation[i] = i+1
return permutation
def multiply(a,b):
output = [0 for i in range(len(a))]
for i in range(len(a)):
output[i] = a[b[i]-1]
return output
def first(thing,x):
for i in range(len(thing)):
if thing[i] == x:
return i
return -1
def findCycle(cycle):
used = [0 for i in range(len(cycle))]
cycles = []
finalStr = ""
while first(used,0) != -1:
this = []
lead = first(used,0)
used[lead] = 1
start = lead+1
this.append(start)
nex = cycle[lead]
while nex != start:
this.append(nex)
nex -= 1
used[nex] = 1
nex = cycle[nex]
cycles.append(this)
for i in cycles:
if len(i) > 1:
finalStr += ("(")
for j in i:
finalStr += (str(j))
finalStr += (")")
return finalStr;
def readInput():
inp = input().split()
n = int(inp[0])
a = inp[1]
b = inp[2]
def createTable():
cyclesList = ["(1)", "(12)" , "(23)", "(13)", "(123)", "(132)"]
#Print a pretty table
width = len(cyclesList)
print ('[Table] |' + '|'.join( cycle.center(7, ' ') for cycle in cyclesList) + '|')
print (57*'-')
for row in cyclesList:
print (row + ":" + ((7-len(row))*' '), end = '');
print ("|", end = '')
for col in cyclesList:
ansStr = findCycle(multiply(parseCycle(row, 3), parseCycle(col, 3)))
print (ansStr.center(7, ' ') + '|', end = '')
print()
def main():
createTable()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment