# 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

I have tried

Range[1,18] + Table[(-i)*2^n, {i, -1, 1}, {n, 0, 1}


because I want $$i = -1$$ while $$n = 0$$ and $$i = 1$$ while $$n = 1$$, but I don't know how to set it up in one line.

• Is this homework?
– ciao
Commented Feb 18, 2020 at 16:00
• Table[(n+3Mod[n,2])/2-1,{n,18}] Commented Feb 19, 2020 at 11:28
• Or Table[Floor[n/2]-(-1)^n,{n,18}] Commented Feb 19, 2020 at 11:34

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. Commented Feb 18, 2020 at 16:15

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

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


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

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


22

If not you can save a few key strokes using

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


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

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


19

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. Commented Feb 18, 2020 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}*)


Update

From this by @azerbajdzan, which introduced me to SubstitutionSystem

SubstitutionSystem[{n_ :> n+1}, {1,0}, 8]//Flatten

(* {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 Commented Feb 23, 2020 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]] Commented Feb 18, 2020 at 21:01
• Sure, but Table was requested in the OP. I originally did it as Range and then changed it. Commented Feb 19, 2020 at 7:38

Using Splice (new in 12.1) - 24 characters

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


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

f[n_] := Riffle[Range[n], Range[0, n - 1]]


f[10] will yield 20 terms.

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

Can crop as desired

a[n_] := 1/2 + (-1)^n  3/2
FoldList[#1 + a[#2] &, 1, Range[20]]


-> {1, 0, 2, 1, 3, 2, 4, 3, 5, 4, 6, 5, 7, 6, 8, 7, 9, 8, 10, 9, 11}

or in one line:

FoldList[#1 + (1 + 3(-1)^#2)/2 &, 1, Range[20]]