# Generating list with short code

I need to get this list

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

with a very small code (< 30 characters) using Table and List functions.

On each step, either it - subtracts 1 or - adds 2

I have tried Range[1,18] + Table[(-i)*2^n, {i, -1, 1}, {n, 0, 1} because I want i = -1 while n = 0 & i = 1 while n = 1

but I don't know how to set it up in one line.

Would be very thankful for any help

Regards,

Well, without Table and such:

Riffle[Range[9], Range[0, 8]]


Or, we can use SequenceFunction:

FindSequenceFunction[Riffle[Range[9], Range[0, 8]]]
(* 1/4 (-1)^#1 (-3 + (-1)^(1 + #1) + 2 (-1)^#1 #1) & *)


So:

1/4 (-1)^#1 (-3 + (-1)^(1 + #1) + 2 (-1)^#1 #1) & /@ Range[18]

• Thank you very much @march. I am just getting started and therefore very thankful for such nice moves. – mvandi607 Feb 18 at 16:15

If you have to use Table you can get the desired result with 23 characters:

Table[## &[i, i - 1], {i, 9}]


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

"Table[##&[i,i-1],{i,9}]" // StringLength


23

If not you can save a few key strokes using

Array[## &[#, # - 1] &, 9]


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

"Array[##&[#,#-1]&,9]" // StringLength


20

or using

## &[#, # - 1] & /@ Range @ 9


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

"##&[#,#-1]&/@Range@9" // StringLength


20

26 characters is the shortest I can think of:

{#, # - 1} & /@ Range@9 // Flatten

• Using Table as requested, Table[{n, n - 1}, {n, 9}] // Flatten is still below 30. – Bob Hanlon Feb 18 at 20:13
f[n_]:= Sequence[n, n - 1]
Array[f, 9]

(*{1, 0, 2, 1, 3, 2, 4, 3, 5, 4, 6, 5, 7, 6, 8, 7, 9, 8}*)


or

Flatten@Array[{#, #-1}&, 9]

(*{1, 0, 2, 1, 3, 2, 4, 3, 5, 4, 6, 5, 7, 6, 8, 7, 9, 8}*)

Flatten@NestList[#+1&,{1,0},8]


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

Array[{#,#-1}&,9,1,Join]


and

Array[{#+1,#}&, 9,0,Join]==Array[{#,#-1}&, 9,1,Join]


Original Post

NestList[#+{1,1}&, {1,0},8]//Flatten

• Two more rather silly ones using Sow, Reap and (recursion with) #0: (i) If[#1[[1]]>9, Nothing, #0[Sow[#1]+1]]&[{1,0}]//Reap//Flatten and (ii) Nest[Sow[#]+1&, {1,0},9]//Reap//Rest//Flatten – user1066 Feb 23 at 14:40

with a very small code (< 30 characters) using Table and List functions.

If requirement to use Table only, then one way is

Flatten@Table[Table[i, {i, j, j - 1, -1}], {j, 1, 9}]


Gives

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


Or this (inspired by the FindSequenceFunction reported by @march):

ClearAll[aList, sf,testList];
aList = {1, 0, 2, 1, 3, 2, 4, 3, 5, 4, 6, 5, 7, 6, 8, 7, 9, 8};
sf = FindSequenceFunction[aList];
testList = sf /@ Table[i, {i, 1, Length[aList]}]
aList == testList
(*{1, 0, 2, 1, 3, 2, 4, 3, 5, 4, 6, 5, 7, 6, 8, 7, 9, 8}*)
(*True*)

• Table[i, {i, 1, Length[aList]}] is equivalent (but potentially slower) than Range[Length[aList]] – b3m2a1 Feb 18 at 21:01
• Sure, but Table was requested in the OP. I originally did it as Range and then changed it. – Mark R Feb 19 at 7:38