# Conditional permutation of combining multiple lists

Let's say I have two lists:

l1={1,2}
l2={a,b}


I want to find legal permutations of combining these two list in a way that the ordering of the members of each list, is preserved. In other words I am looking for a function ConditionalPermutation such that:

ConditionalPermutation[{1,2},{a,b}]=
{{1,2,a,b},{1,a,2,b},{a,b,1,2},{a,1,b,2},{1,a,b,2},{a,1,2,b}}


as you can see, in this simple case, we have 4 elements altogether, so the total number of permutations are 4*3*2=12. Out of these half have 2 appearing before 1 and also half have b appearing before a. Therefore the list of interest has (24/2)/2=6 legal permutations. In general there could be multiple l_i lists and each would have a different size.

I can code it with an imperative implementation, but I am looking for a neater way of doing it in Mathematica.

Ideally I want the solution to accept a general condition, or potentially one for each input list.

• Just curious...what is "an imperative implementation" ? – JimB Nov 17 '16 at 23:53
• as in like C++ code. not taking advantage of any of the higher order list manipulation functions in Mathematica. – Shb Nov 17 '16 at 23:55
• Thanks. I'm not a C++ programmer and hadn't come across that term before. – JimB Nov 17 '16 at 23:57
• Imperative is not a C++ term. it's basically the traditional form of writing programs. like writing for loops etc. One high level approach would be to create the full list and then filter it according to some conditions, but although that might look pretty, it's probably not very efficient. – Shb Nov 17 '16 at 23:58
• I thought that type of coding was called procedural. – march Nov 18 '16 at 0:34

We have a list of lists. The function listNumbersToElements here takes such a list, along with a set of indices into it. For each index, it takes the next element from the correct list.

ClearAll[listNumbersToElements];
listNumbersToElements[inds_List, allLists_List] := Block[{c},
c[_] = 0;
Function[{listIndex},
++c[listIndex]; allLists[[listIndex, c[listIndex]]]] /@ inds]


Example:

In:= listNumbersToElements[{1, 2, 1, 2}, {{1, 2}, {a, b}}]
listNumbersToElements[{1, 1, 2}, {{1, 2}, {a, b}}]
listNumbersToElements[{2, 2, 1, 1}, {{1, 2}, {a, b}}]

Out= {1, a, 2, b}

Out= {1, 2, a}

Out= {a, b, 1, 2}


This function takes the list of lists, fills a list with n copies of each list's index, where n is the length of the respective list, then creates all permutations of the list of indices. Then it applies listNumbersToElements to each permutation, getting the final result as desired.

ClearAll[conditionalPermutation];
conditionalPermutation[lists__List] :=
Module[{argsAsList = List@lists},
listNumbersToElements[#, argsAsList] & /@ Permutations[Flatten[
MapIndexed[Table[First[#2], Length[#1]] &, argsAsList],
1]]]


Examples:

In:= conditionalPermutation[{1, 2}, {a, b}]
conditionalPermutation[{1, 2}, {a, b, c}]

Out= {{1, 2, a, b}, {1, a, 2, b}, {1, a, b, 2}, {a, 1, 2, b}, {a,
1, b, 2}, {a, b, 1, 2}}

Out= {{1, 2, a, b, c}, {1, a, 2, b, c}, {1, a, b, 2, c}, {1, a,
b, c, 2}, {a, 1, 2, b, c}, {a, 1, b, 2, c}, {a, 1, b, c, 2}, {a, b,
1, 2, c}, {a, b, 1, c, 2}, {a, b, c, 1, 2}}

• I get an empty list as the result. It does not appear that l (lowercase L) is defined in Length[l]. Am I missing something? – JimB Nov 18 '16 at 2:44
• @Jim thanks for catching that. should be fixed now. accidentally copied an old cell from the notebook. – Michael Curry Nov 18 '16 at 3:18
• +1 Very nice and elegant. And works for multiple lists! – JimB Nov 18 '16 at 3:28
• @MichaelCurry beautiful! Out of curiosity, How would you modify conditionalPermutation if you wanted it to accept a general condition, say have the elements in the opposite order, or have the elements from the lists separated by elements from another list, so that say only {1,a,2,b} and {a,1,b,2} would be legal. – Shb Nov 18 '16 at 12:54
• hmmm. changes to the ordering would involve changes to listNumbersToElements: basically have it pick out the elements in some other order rather than just first to last. or just reverse the input list. generating all sets satisfying a partial ordering would require more substantial changes – Michael Curry Nov 18 '16 at 14:13

This is an extended comment in that it gives (I hope) the number of arrangements rather than the arrangements. However, I think the steps here will suggest a way to obtain the arrangements:

a = {a1, a2};
b = {b1, b2, b3, b4};
na = Length[a];
nb = Length[b];
ip = IntegerPartitions[nb, Min[na + 1, nb]]
(* {{4},{3,1},{2,2},{2,1,1}} *)
Sum[Binomial[na + 1, Length[ip[[i]]]] Length[Permutations[ip[[i]]] ], {i, Length[ip]}]
(* 15 *)


The logic is that there will be na+1 places to put the numbers from b:

  x  a1  x  a2  x


For this example there are 3 places (na+1) to put items from b. So we look at all of the Min[na+1,nb] integer partitions of b and weight those by the number of associated permutations.