4
$\begingroup$

This seems like a very inefficient way of doing what I want. I generate all permutations of (for example) {1, 1, 2, 2}, then for each permutation I generate its rotations, select the first one in Sort order, and use that for GatherBy.

list = {1, 1, 2, 2};
display@GatherBy[Permutations@list, Sort[Table[RotateLeft[#, i], {i, 0, Length@# - 1}]][[1]]& ]
(* {{"1122", "1221", "2112", "2211"}, {"1212", "2121"}} *)

How can I do this more efficiently?

$\endgroup$

2 Answers 2

4
$\begingroup$
GroupOrbits[CyclicGroup[Length @ #], Permutations @ #, Permute] & @ list
{{{1, 1, 2, 2}, {1, 2, 2, 1}, {2, 1, 1, 2}, {2, 2, 1, 1}}, 
 {{1, 2, 1, 2}, {2, 1, 2, 1}}}
$\endgroup$
3
$\begingroup$
With[{p = Permutations[list]}, Extract[p, #] & /@
  ConnectedComponents[MapIndexed[UndirectedEdge,
    Lookup[PositionIndex[p], RotateLeft[p, {0, 1}]]]]]

{{{1, 1, 2, 2}, {1, 2, 2, 1}, {2, 1, 1, 2}, {2, 2, 1, 1}}, {{1, 2, 1, 2}, {2, 1, 2, 1}}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.