-
Notifications
You must be signed in to change notification settings - Fork 0
/
cycle.py
68 lines (51 loc) · 1.73 KB
/
cycle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Perm:
def __init__(self, *inp):
self.range = []
max_elem = max(inp)
self.size = max_elem
current_elem = inp[0]
for i in range(max_elem):
self.range.append(i+1)
for i in range(1,len(inp)):
self.range[current_elem-1] = inp[i]
current_elem = inp[i]
self.range[current_elem-1] = inp[0]
def __repr__(self):
rpr = ""
for i in range(len(self)):
rpr += "%d ---- %d\n" % (i+1, self.range[i])
return rpr[:-1]
def __len__(self):
return self.size
def extend(self, to_size):
if len(self) < to_size:
extension = list(range(len(self)+1, to_size+1))
self.range.extend(extension)
self.size = to_size
def __mul__(self, other):
c = Perm(1)
common_length = max(len(self),len(other))
self.extend(common_length)
other.extend(common_length)
c.extend(common_length)
for i in range(len(c)):
c.range[i] = other.range[self.range[i]-1]
return c
def cycle(self):
cyc = ""
def one_cyc(index):
cyc_term = "Perm(" + str(index + 1)
next_term = self.range[index]
while next_term != (index + 1):
cyc_term += ',' + str(next_term)
next_term = self.range[next_term-1]
return cyc_term + ')'
cycles = 0
for i in range(len(self)):
if (i+1) != self.range[i] and not str(i+1) in cyc:
if cycles == 0:
cyc += one_cyc(i)
else:
cyc += "*" + one_cyc(i)
cycles += 1
return cyc