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
6
$\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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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