-3
\$\begingroup\$

Challenge

Your task is to generate a string using this sequence:

1,1,2,1,1,2,3,2,1,1,3,2,4,2,3,1,....

Which is more understandable in this format:

1
1 2 1
1 2 3 2 1
1 3 2 4 2 3 1
1 4 3 2 5 2 3 4 1

The pattern increases the number of Digits in the string by two for each increment in the Value of N

For each integer value up to the total N, aside from 1 the string is centred on the current value of N and bookended by the previous values of N in decreasing order on the left, and increasing order on the right - with the exception of the value 1, which bookends the entire string.

Input

Number of steps

Output

The series separated by comma

\$\endgroup\$
18
  • 4
    \$\begingroup\$ What is the expected output for \$n=3\$? [1,1,2,1,1,2,3,2,1]? [[1],[1,2,1],[1,2,3,2,1]]? [1,2,3,2,1]? Or any of those? \$\endgroup\$
    – Arnauld
    Mar 16, 2020 at 10:15
  • 4
    \$\begingroup\$ I suggest removing the "separated by a comma" text, which appears to override site defaults - most languages will at least wrap a list in some kind of brackets and many will delimit with both space and comma, some may use semi-colons or just spaces. We prefer not to ask answerers to format their output unless it's a core part of the challenge, and as you can see none of the six answers (including mine) have actually followed this instruction. \$\endgroup\$ Mar 16, 2020 at 11:09
  • 4
    \$\begingroup\$ @RGS it seems to be [1, n -1, n - 2, n - 3, ..., 2, n, 2, ..., n - 3, n - 2, n - 1, 1] if I've understood it correctly. \$\endgroup\$ Mar 16, 2020 at 11:19
  • 6
    \$\begingroup\$ This description: A middle number is incremented by 1 and being surrounded by its previous number. in your challenge does not appear accurate with regards to the sequence provided. Additionally, it would be more accurate to say the number of digits in the string increments by 2 as opposed to Here is a pattern that numbers in each step is increasing by 2 \$\endgroup\$
    – T3RR0R
    Mar 16, 2020 at 12:34
  • 3
    \$\begingroup\$ @WasifHasan You need to edit the challenge incorporating those comments. Then I (and I'm sure others) will vote to reopen. \$\endgroup\$
    – Noodle9
    Mar 16, 2020 at 17:15

7 Answers 7

3
\$\begingroup\$

05AB1E, 7 bytes

Port of the Jelly answer.

L¦ÁR1šû

Try it online!

Explanation

L       1 .. N
 ¦      Tail
  Á     Shift right
   R    Reverse
    1š  Prepend 1
      û Palindromize

05AB1E, 9 bytes

If the whole format is necessary, here's one with 9 bytes. (Question: Does 05AB1E have any sort of implicit range?)

LεL¦ÁR1šû

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ To myself 2 months ago. Yes there is, take a look at the Loops section in the 05AB1E wiki. But it doesn't seem to work. \$\endgroup\$
    – user92069
    May 23, 2020 at 23:26
2
\$\begingroup\$

Jelly, 10 10 ?8 bytes

Ḋṙ-U1;ŒB

Try it online!

I’m assuming you want the \$n\$th row of the pyramid. If you actually want the full sequence for the \$n\$th step, then it would be 10 bytes.

A monadic link taking an integer and returning a list of integers.

Explanation

Ḋ        | List 1..(n-1)
 ṙ-      | Rotate right 1
   U     | Reverse list
    1;   | Prepend 1
      ŒB | Bounce (mirror
\$\endgroup\$
2
\$\begingroup\$

Python 3.8 (pre-release), 86 81 58 53 bytes

lambda n:[[1],[1,*(l:=range(2,n))[::-1],n,*l,1]][n>1]

Try it online! Hats off to @Noodle9 for saving me a whooping 23 bytes and thanks to @squid for saving me 5 bytes!

\$\endgroup\$
5
  • 1
    \$\begingroup\$ 58 bytes \$\endgroup\$
    – Noodle9
    Mar 18, 2020 at 12:33
  • \$\begingroup\$ @Noodle9 I was so sure the f-string formatting was going to be the way to go... xD \$\endgroup\$
    – RGS
    Mar 18, 2020 at 13:04
  • 1
    \$\begingroup\$ 53 bytes \$\endgroup\$
    – squid
    Mar 19, 2020 at 13:48
  • \$\begingroup\$ @squid nice one, doing the *l inside the list to return! \$\endgroup\$
    – RGS
    Mar 19, 2020 at 13:50
  • \$\begingroup\$ Far too much later, another -3 \$\endgroup\$
    – squid
    Aug 24, 2020 at 18:32
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 145 bytes

n=>{var s= "";for(int i=0;i++<n;){for(int j=1;++j<=i;)s+=(j>2?i-j+2:1)+",";s+=i;for(int j=i;j-->1;)s+=","+(j>1?i-j+1:j);s+=i<n?",":"";}return s;}

Try it online!

Function generating whole series.

\$\endgroup\$
1
1
\$\begingroup\$

Jelly, 11 bytes

(9 if we only require the \$n^{\text{th}}\$ row of the "understandable format" - remove )F)

,2œṖRUFŒB)F

A monadic link accepting an integer, the number of steps, which yields a list of integers, the series up to and including that step.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Erlang (escript), 182 bytes

Unfortunately this became the longest solution over here...

q(I)->lists:join(" ",[integer_to_list(X)||X<-lists:seq(2,I-1)]).
x(0)->"";x(I)->x(I-1)++"\n"++if I>1->"1 "++lists:reverse(q(I))++" "++integer_to_list(I)++" "++q(I)++" 1";1<2->"1"end.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6),  68  65 bytes

Returns the whole sequence after \$n\$ iterations.

f=n=>--n?[...f(n),1,...(g=k=>k>1?[k,...g(k-1),k]:[n+1])(n),1]:[1]

Try it online!


JavaScript (ES6),  53  46 bytes

Returns the \$n\$th row of the pyramid.

n=>(g=k=>k>1?[k%n||1,...g(k-1),k%n||1]:[n])(n)

Try it online!

\$\endgroup\$

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