Can you fix it?
Lets fix it with a procedural benadonC
Clear[benadonSort, benadonC]
benadonC[l_List?VectorQ] := Block[{o1 = 1, cache = {}, r = {}},
For[o = 1, o <= Length[l], o++,
If[IntersectingQ[cache, {l[[o]]}],
r = Join[r, {{o1, o - 1}}];
cache = {l[[o]]};
o1 = o, (* else *)
cache = Join[cache, {l[[o]]}]
]
];
Join[r, {{o1, Length[l]}}]
]
benadonSort[l1_List?VectorQ, l2_List?VectorQ, l3_List?VectorQ] :=
Block[{i, v},
{v, i} = Transpose[
SortBy[
Join[
Transpose[{l1, ConstantArray[1, Length[l1]]}],
Transpose[{l2, ConstantArray[2, Length[l2]]}],
Transpose[{l3, ConstantArray[3, Length[l3]]}]
], First
]
];
Part[v, #]& /@ (Span @@@ benadonC[i])
] /; Length[l1] > 0 && Length[l2] > 0 && Length[l3] > 0
and test a bit more than before
In[88]:= benadonSort[r, s, t]
Out[88]= {{0, 1, 2}, {3, 4}, {5, 7, 8}, {9}}
In[89]:= benadonSort[{1, 3, 8, 3}, s, t]
Out[89]= {{0, 1, 2}, {3}, {3, 4}, {5, 7, 8}, {9}}
In[92]:= benadonSort[{1, 3, 8, 3}, Join[s, {7}], t]
Out[92]= {{0, 1, 2}, {3} 7}, {7, 8, 9}}
In[93]:= benadonSort[{1, 3, 8, 3}, Join[s, {7, 7}], t]
Out[93]= {{0, 1, 2}, {3}, {3, 4}, {5, 7}, {7}, {7, 8, 9}}
In[90]:= benadonSort[{1, 1, 1, 1}, {2, 2, 2, 2, 2, 2}, {3, 3}]
Out[90]= {{1}, {1}, {1}, {1, 2}, {2}, {2}, {2}, {2}, {2, 3}, {3}}
Jobs for the week-end:
- enhance
benadonSort
to run with an arbitrary finite positive number of non-empty lists
- make
benadonC
functional