# How to efficiently replace the repetitive sequence?

The problem is how to determine the repetitive sequences and replace the part with consecutive sequences For example: A={{1,3,4},{2,3,5},{1,6}} Then, detect there are the repetitive case for 1 and 3 and become A={{1,4,6},{3,5,7},{2,8}}

Q1:How can I assign the repetitive sequences in lists(transpose tableform) with the rules:

-Highest level & no blocked on the top will be assigned lower number.

-High level with similar repetition will follow later.

-The one underneath the other number will be much later. For example:Input

A={{2,1},{2,2},{1,2},{1},{2,1,1}};
Print[Rotate[
Grid[Map[Rotate[#, -90 Degree] &, A, {-1}], Frame -> All],
90 Degree]]


And implement the rules to get the output:

Q2:How can I generate all the possible list outcomes without the rules when the lists have repetitive cases?

• This is usually called 'standardization' in combinatorics. Commented Apr 1, 2021 at 14:18

Update:

ClearAll[addIndices, addOrderings, standardize]


Append to each entry of the input list its index and the length of its parent sublist:

addIndices[a_] := {#, #4, {#2, #3}} & @@@ Join @@
(Flatten /@ Thread[{#, Length @ #}] & /@ MapIndexed[List, a, {2}]);


Use SortBy[{First, f2, f3,...}] to sort the annotated list using sorting functions f2, f3,..., that represent your rules, and add the orderings obtained to the annotated list.

In the implementation below, the sorting functions #[[2]] - #[[3, 2]] & and -#[[2]] & capture your rules:

addOrderings = MapIndexed[{#[[1]], #2[[1]], #[[3]]} &]@
SortBy[{First, #[[2]] - #[[3, 2]] &, -#[[2]] &}]@# &;


Compose the addOrderings with addIndices:

standardize = addOrderings @* addIndices;


to generate a list of triples {a, number_assigned_to_a, {row, column}}.

Example:

standardize[A2]

 {{1, 1, {5, 3}},
{1, 2, {1, 2}},
{1, 3, {4, 1}},
{1, 4, {5, 2}},
{1, 5, {3, 1}},
{2, 6, {2, 2}},
{2, 7, {3, 2}},
{2, 8, {1, 1}},
{2, 9, {2, 1}},
{2, 10, {5, 1}}}

DeleteCases[0] /@ Normal[SparseArray[#3 -> #2 & @@@ standardize[A2]]]

 {{8, 2}, {9, 6}, {5, 7}, {3}, {10, 4, 1}}

SparseArray[Reverse@#3 -> # & @@@ standardize[A2]] // Reverse //
Grid[Normal[#] /. 0 -> "", Frame -> All] &


SparseArray[Reverse@#3 -> #2 & @@@ standardize[A2]] // Reverse //
Grid[Normal[#] /. 0 -> "", Frame -> All] &


ClearAll[f]
f = TakeList[Ordering@Ordering@Flatten@#, Length /@ #] &;


Examples:

A1 = {{1, 3, 4}, {2, 3, 5}, {1, 6}};

f @ A1

 {{1, 4, 6}, {3, 5, 7}, {2, 8}}

A2 = {{2, 1}, {2, 2}, {1, 2}, {1}, {2, 1, 1}};

f @ A2

{{6, 1}, {7, 8}, {2, 9}, {3}, {10, 4, 5}}


Update:

ClearAll[pad]

pad @ A2 // Grid[#, Frame -> All] &


If we use f on pad @ A non-numeric entries will be assigned numbers greater than Length@Flatten@A, Removing non-numeric elements from f @ pad @ A gives the desired result:

f[pad @ A2] /. x_ /; x > Length[Flatten @ A2] -> "" // Grid[#, Frame -> All] &


ClearAll[g]
g[a_] := Select[# <= Length @ Flatten @ a &] /@ Transpose @ Reverse @ f @ pad @ a

g @ A2

{{8, 2}, {9, 6}, {4, 7}, {5}, {10, 3, 1}}

• If your version of Mathematica does not have TakeList then try TakeList = If[#2 == {}, #1, Prepend[#0[Drop[#1, First@#2], Rest@#2], Take[#1, First@#2]]] &; Commented Mar 16, 2021 at 20:51
• How can I efficiently assign the sequence for repeated cases(like randomly assigned or specific method), for example when there are 4 or even more number of no. 1 in the list, so that when I retrieve each number according to the sequences will be more effective by reducing the blocks on each targeted no. in tableform-transpose. Commented Mar 31, 2021 at 11:13
• @tjx6499, the method should work for any number of repetitions. If you have an example where the method fails, please update your question with the example input and desired output.
– kglr
Commented Mar 31, 2021 at 11:33
• @kglr, how can I get the outcome in list form={{8,2},{9,6},{4,7},{5},{10,3,1}}? Commented Apr 1, 2021 at 12:11
• @kglr, it is possible to get the same desired result with rules that I stated in my question={{8, 2}, {9, 6}, {5, 7}, {3}, {10, 4, 1}}? Commented Apr 1, 2021 at 14:06