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?
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} *)
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}
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
lst//Table[Sequence@@Range[0,4],Length@Split[#,Less]]&
{0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4}
lst//Table[Splice@Range[0,4],Length@Split[#,Less]]&
$\endgroup$
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} *)
SequenceReplace[#, {x__} /; (Less@x) :> Sequence @@ Range[0, Max@#]] & @ list
{0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4}
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?
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}
Module[{r = Range @@ MinMax[list]}, Flatten@Table[r, Ceiling[Length[list]/Length[r]]]]
$\endgroup$