5
$\begingroup$

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$ and $i = 1$ while $n = 1$, but I don't know how to set it up in one line.

$\endgroup$
3

10 Answers 10

13
$\begingroup$

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]
$\endgroup$
1
  • 1
    $\begingroup$ Thank you very much @march. I am just getting started and therefore very thankful for such nice moves. $\endgroup$
    – mvandi607
    Commented Feb 18, 2020 at 16:15
9
$\begingroup$

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

$\endgroup$
7
$\begingroup$

26 characters is the shortest I can think of:

{#, # - 1} & /@ Range@9 // Flatten
$\endgroup$
1
  • 5
    $\begingroup$ Using Table as requested, Table[{n, n - 1}, {n, 9}] // Flatten is still below 30. $\endgroup$
    – Bob Hanlon
    Commented Feb 18, 2020 at 20:13
7
$\begingroup$
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}*)
$\endgroup$
7
$\begingroup$

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

Original Answer

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

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

In addition

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
$\endgroup$
1
  • $\begingroup$ 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 $\endgroup$
    – user1066
    Commented Feb 23, 2020 at 14:40
4
$\begingroup$

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}
$\endgroup$
3
$\begingroup$

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*)
$\endgroup$
2
  • $\begingroup$ Table[i, {i, 1, Length[aList]}] is equivalent (but potentially slower) than Range[Length[aList]] $\endgroup$
    – b3m2a1
    Commented Feb 18, 2020 at 21:01
  • $\begingroup$ Sure, but Table was requested in the OP. I originally did it as Range and then changed it. $\endgroup$
    – Mark R
    Commented Feb 19, 2020 at 7:38
2
$\begingroup$

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}

$\endgroup$
2
$\begingroup$
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

$\endgroup$
1
$\begingroup$
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]]
$\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.