5
$\begingroup$

Given a list such as lis={1,2,3} how can one generate all permutations but as list, like this (order matters)

{{ 1,2,3 },   (*First level can be found using Permutations[{1,2,3}]*)
 { 1,3,2 },
 { 2,1,3 },
 { 2,3,1 },
 { 3,1,2 },
 { 3,2,1 },

 { {1}, {2,3} },  (*but what about these?*)
 { {1}, {3,2} },
 { {2}, {1,3} },
 { {2}, {3,1} },
 { {3}, {1,2} },
 { {3}, {2,1} },
 { {1,2}, {3} },
 { {2,1}, {3} },
 { {1,3}, {2} },
 { {3,1}, {2} },
 { {2,3}, {1} },
 { {3,2}, {1} },

 { {1},{2},{3}},  (*last level, can be obtained from first level *)
 { {1},{3},{2}},  (*by making each element in that, as a list*)
 { {2},{1},{3}},
 { {2},{3},{1}},
 { {3},{1},{2}},
 { {3},{2},{1}},

}

It is similar to Permutations but with partition to lists as well. I looked at Subsets, Partition and number of posts, but did not see anything similar.

$\endgroup$
0

2 Answers 2

7
$\begingroup$

You may use Permutations and IntegerPartitions with TakeList.

With

permutationsList[list_] :=
 Flatten[#, 1] &@
  Through@(
     Function[{p}, Map[TakeList[#, p] &]] /@
      Sort@Flatten[Permutations /@ IntegerPartitions@Length@list, 1]
     )@Permutations[list]

Then

permutationsList@Range@3 //Column
{{1,2,3}}
{{1,3,2}}
{{2,1,3}}
{{2,3,1}}
{{3,1,2}}
{{3,2,1}}
{{1},{2,3}}
{{1},{3,2}}
{{2},{1,3}}
{{2},{3,1}}
{{3},{1,2}}
{{3},{2,1}}
{{1,2},{3}}
{{1,3},{2}}
{{2,1},{3}}
{{2,3},{1}}
{{3,1},{2}}
{{3,2},{1}}
{{1},{2},{3}}
{{1},{3},{2}}
{{2},{1},{3}}
{{2},{3},{1}}
{{3},{1},{2}}
{{3},{2},{1}}

Hope this helps.

$\endgroup$
4
$\begingroup$

There are 24 permutations to be chosen in groups of six in the original query.

alist = {1, 2, 3}

s = Permutations[alist, {3}]

{{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}

x1 = Map[Partition[#, UpTo[1]] &, s]

{{{1}, {2}, {3}}, {{1}, {3}, {2}}, {{2}, {1}, {3}}, {{2}, {3}, {1}}, \
{{3}, {1}, {2}}, {{3}, {2}, {1}}}

x2 = Map[Partition[#, UpTo[2]] &, s]

   {{{1, 2}, {3}}, {{1, 3}, {2}}, {{2, 1}, {3}}, {{2, 3}, {1}}, {{3, 
   1}, {2}}, {{3, 2}, {1}}}

x3 = Map[Partition[#, UpTo[3]] &, s]

{{{1, 2, 3}}, {{1, 3, 2}}, {{2, 1, 3}}, {{2, 3, 1}}, {{3, 1, 2}}, {{3,
        2, 1}}}

Join[x1, x2, x3]

Gets the job done using the UpTo specification. x2 will need to be reversed and added to the final list.

$\endgroup$
0

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.