Last active
January 14, 2022 01:42
-
-
Save tonicanada/1fcdcd2098167bc09b257fea74940a1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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