def _cycle_permute(l):
 if (len(l) == 1):
  return l
 min_item = min(l, key=default_sort_key)
 indices = [i for (i, x) in enumerate(l) if (x == min_item)]
 le = list(l)
 le.extend(l)
 indices.append((len(l) + indices[0]))
 sublist = [[le[indices[i]:indices[(i + 1)]]] for i in range((len(indices) - 1))]
 idx = sublist.index(min(sublist))
 ordered_l = le[indices[idx]:(indices[idx] + len(l))]
 return ordered_l

def _cycle_permute ( l ) : if ( len ( l ) == 1 ) : return l min_item = min ( l , key = default_sort_key ) indices = [ i for ( i , x ) in enumerate ( l ) if ( x == min_item ) ] le = list ( l ) le . extend ( l ) indices . append ( ( len ( l ) + indices [ 0 ] ) ) sublist = [ [ le [ indices [ i ] : indices [ ( i + 1 ) ] ] ] for i in range ( ( len ( indices ) - 1 ) ) ] idx = sublist . index ( min ( sublist ) ) ordered_l = le [ indices [ idx ] : ( indices [ idx ] + len ( l ) ) ] return ordered_l
cyclic permutations based on canonical ordering this method does the sort based ascii values while a better approach would be to used lexicographic sort .