5
$\begingroup$

I have a list like this:

{0,1,2,3,4,0,1,3,4,0,1,3}

This list must have a sequence of numbers from 0 to 4 repeatedly but it lacks of some numbers. How can I put the lacking numbers and hold the order of the sequences?

$\endgroup$
4
  • 2
    $\begingroup$ What do you know, and what don't you know, about the list in the general case? If you know that you want a list repeating 0,1,2,3,4 over an over again for n times, why don't you just create this list instead of taking an incomplete list where you need to fill in the missing numbers...? $\endgroup$
    – a20
    May 14, 2020 at 20:15
  • 1
    $\begingroup$ Along with what @a20 is asking: more context is needed here $\endgroup$ May 14, 2020 at 20:27
  • $\begingroup$ Module[{r = Range @@ MinMax[list]}, Flatten@Table[r, Ceiling[Length[list]/Length[r]]]] $\endgroup$
    – Bob Hanlon
    May 14, 2020 at 20:36
  • $\begingroup$ what happens after the ending 3? does the list stop or do we have to add 4? $\endgroup$
    – Alucard
    May 14, 2020 at 20:43

8 Answers 8

5
$\begingroup$

Given

seq = Range[0, 4]
list = {0, 1, 2, 3, 4, 0, 1, 3, 4, 0, 1, 3}

and

fill[seq_, list_] := Flatten[seq & /@ Split[list, Less]]

then

fill[seq, list]
(* {0,1,2,3,4,0,1,2,3,4,0,1,2,3,4} *)

and also

fill[seq, {1,1,1,3,1,1}]
(* {0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,0,1,2,3,4} *)
$\endgroup$
4
$\begingroup$
ls = {0, 1, 2, 3, 4, 0, 1, 3, 4, 0, 1, 3};
Table[Splice@Range[0, 4], Max[Last /@ Tally[ls]]]

{0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4}

or using SequenceReplace

ls = {0, 1, 2, 3, 4, 0, 1, 3, 4, 0, 1, 3};
If[ls[[-1]] =!= 4, ls = Join[ls, {4}]]; 
SequenceReplace[ls, {n0 : 0 | 1 | 2 | 3, n1_} /;n1 != n0 + 1 :> Sequence[n0, n0 + 1, n1]]

{0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4}

$\endgroup$
2
$\begingroup$
lst = {0, 1, 2, 3, 4, 0, 1, 3, 4, 0, 1, 3}
fun[{a_, b_}] := If[a > b, {a, b}, If[Range[a, b] == {a, b}, {a, b}, Range[a, b]]]
fun[#] & /@ Partition[lst, 2] // Flatten
$\endgroup$
2
$\begingroup$
lst//Table[Sequence@@Range[0,4],Length@Split[#,Less]]&


{0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4}
$\endgroup$
1
  • $\begingroup$ In v12.1, lst//Table[Splice@Range[0,4],Length@Split[#,Less]]& $\endgroup$
    – user1066
    May 25, 2020 at 13:40
1
$\begingroup$

Consider building your own list of whichever length you want instead:

listLength = 12;
PadRight[{}, listLength, Range[0, 4]]

(* Out: {0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1} *)
$\endgroup$
1
$\begingroup$
SequenceReplace[#, {x__} /; (Less@x) :> Sequence @@ Range[0, Max@#]] & @ list
{0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4}
$\endgroup$
1
$\begingroup$

Is this possibly what you mean?

list = {0, 1, 2, 3, 4, 0, 1, 3, 4, 0, 1, 3};
Flatten@Join[list, 
  Complement[Range @@ MinMax@list, #] & /@ Split[list, Less]]

(*{0, 1, 2, 3, 4, 0, 1, 3, 4, 0, 1, 3, 2, 2, 4}*)

This seems to satisfy:

How can I put the lacking numbers and hold the order of the sequences?

$\endgroup$
1
$\begingroup$
list = {0, 1, 2, 3, 4, 0, 1, 3, 4, 0, 1, 3};

Using SequenceCount

c = SequenceCount[list, x_ /; Less @@ x]

3

Table[Splice @* Apply[Range] @ MinMax @ list, c]

{0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4}

$\endgroup$

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.