Skip to content

Instantly share code, notes, and snippets.

@tonicanada
Last active January 14, 2022 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonicanada/1fcdcd2098167bc09b257fea74940a1d to your computer and use it in GitHub Desktop.
Save tonicanada/1fcdcd2098167bc09b257fea74940a1d to your computer and use it in GitHub Desktop.
test_case = [3, 5, 2, 4, 7, 8, 1, 6]
expected_result = [[1, 3, 2, 5, 7], [4], [6, 8]]
def first_idx_unseen(seen_array):
for i in range(len(seen_array)):
if seen_array[i] == False:
return i
def order_cycle(cycle):
min_value_idx = cycle.index(min(cycle))
return cycle[min_value_idx:] + cycle[:min_value_idx]
def cycle_notation(permutation):
result = []
cycle = []
idx = 0
idx_to_check = len(permutation)
seen = [False] * len(permutation)
while idx_to_check > 0:
while (seen[idx] == False):
cycle.append(permutation[idx])
seen[idx] = True
idx = permutation[idx] - 1
idx_to_check -= 1
result.append(order_cycle(cycle))
cycle = []
idx = first_idx_unseen(seen)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment