Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_cycles(a):
- n = len(a)
- used = [0] * n
- cycles = []
- for i in range(n):
- if not used[i]:
- nw = []
- nxt = i
- while not used[nxt]:
- nw.append(a[nxt])
- used[nxt] = 1
- nxt = a[nxt] - 1
- if nw:
- cycles.append(nw)
- return cycles
- def rev(a):
- n = len(a)
- nw = [0] * n
- for i in range(n):
- nw[a[i] - 1] = i + 1
- return nw
- a = [4, 7, 3, 1, 6, 5, 8, 2]
- b = [4, 6, 3, 2, 8, 7, 1, 5]
- c = [7, 8, 2, 3, 6, 5, 4, 1]
- print(get_cycles(a))
- print(rev(b))
- print(get_cycles(rev(b)))
- print(get_cycles(c))
Advertisement
Add Comment
Please, Sign In to add comment