1
$\begingroup$

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}}

Updates

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. enter image description here 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]]

enter image description here

And implement the rules to get the output:

enter image description here

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

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

1 Answer 1

3
$\begingroup$

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] & 

enter image description here

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

enter image description here

Original answer:

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 = Reverse[Transpose[PadRight[#, Automatic, ""]]] &;

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

enter image description here

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] &

enter image description here

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}}
$\endgroup$
10
  • 1
    $\begingroup$ 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]]] &; $\endgroup$
    – Somos
    Commented Mar 16, 2021 at 20:51
  • $\begingroup$ 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. $\endgroup$
    – tjx6499
    Commented Mar 31, 2021 at 11:13
  • $\begingroup$ @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. $\endgroup$
    – kglr
    Commented Mar 31, 2021 at 11:33
  • $\begingroup$ @kglr, how can I get the outcome in list form={{8,2},{9,6},{4,7},{5},{10,3,1}}? $\endgroup$
    – tjx6499
    Commented Apr 1, 2021 at 12:11
  • $\begingroup$ @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}}? $\endgroup$
    – tjx6499
    Commented Apr 1, 2021 at 14:06

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.