Restricting the input to lists of integers seems unnecessary to me, This can for example be fixed by checking len(in_perm) = len(in_perm) = len(set(in_perm)) Returns True (and from_permutation_to_disjoints_cycles(, ])Īborts later with a ValueError because some index is not found). Is_valid_permutation() does not verify if the elements in the two listsĪre unique. Return merge_decoupled_permutation(decouple_permutation(perm))ĭef from_disjoint_cycles_to_permutation(dc): Raise IOError('Input permutation is not valid') Index_next = for k in decoupled].index(decoupled) + 1ĭecoupled.append(decoupled)ĭef from_permutation_to_disjoints_cycles(perm): Return, perm) if perm != perm] if a != a]ĭef merge_decoupled_permutation(decoupled): Return lift_list(input_list) + (lift_list(input_list) if len(input_list) > 1 else ) \ :return: eliminates the first nesting levels of lists. List of nested lists becomes a list with the element exposed in the main list. If not all(isinstance(n, int) for n in in_perm):
Means permute 1 with 2, 2 with 3, 3 with 1. def is_valid_permutation(in_perm):Ī permutation is a list of 2 lists of same size: Means permute 1 with 3, 3 with 2, 2 with 1 and 4 with 6, leaving 5 alone.Ĭorresponding disjoint cycles are: c =, ]
What I could not answer is: how to make it more efficient? Should this be made recursive? Why or why not?Ĭauchy notation p =, ] The code below provides a possible answer to the problem: how to go from the Cauchy notation to the disjoint cycle and backward? Given an element of the permutation group, expressed in Cauchy notation, it is often useful to have it expressed in disjoint cycles (for example to apply the permutation to the keys of a dictionary).