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 & 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,

$\endgroup$
3
12
$\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 Feb 18 '20 at 16:15
8
$\begingroup$

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

$\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 Feb 18 '20 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$
6
$\begingroup$
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 Feb 23 '20 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 Feb 18 '20 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 Feb 19 '20 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.