6
\$\begingroup\$

This question already has an answer here:

Challenge

Given a positive integer \$n\$, output the \$n\$-dimensional pyramidal list.

Example


\$n = 1\$:

enter image description here

Objects arranged in a 1D pyramid (line) with side length 1 is just by itself.

So, the output is {1}.


\$n = 2\$:

enter image description here

Objects arranged in a 2D pyramid (a triangle) with side length 2 would have one on the first row (cyan), and two on the second row (magenta).

Note, the first row is a 1D pyramid with side length 1 ({1}), and the second row is a 1D pyramid with side length 2 ({1, 1}).

So, the output is {{1}, {1, 1}}.


\$n = 3\$:

enter image description here

Objects arranged in a 3D pyramid with side length 3 would have 3 layers:

  • The first layer (cyan) is a 2D pyramid with side length 1. It has one row, which has one object. ({{1}})
  • The second layer (magenta) is a 2D pyramid with side length 2; It has two rows: the first row with one object and the second row with two objects. ({{1}, {1, 1}})
  • The third layer (yellow) is a 2D pyramid with side length 3; it has three rows: the first row with one object, second with two, and third with three. ({{1}, {1, 1}, {1, 1, 1}})

So, the output is {{{1}}, {{1}, {1, 1}}, {{1}, {1, 1}, {1, 1, 1}}}.


\$n = k\$

This is a \$k\$ dimensional pyramid with side length \$k\$. Each "layer" would be a \$k-1\$ dimensional pyramid, whose side length is its index (1-indexed).


Sample Outputs

n  output

1  {1}

2  {{1},
    {1, 1}}

3  {{{1}},
    {{1}, {1, 1}},
    {{1}, {1, 1}, {1, 1, 1}}}

4  {{{{1}}},                     
    {{{1}}, {{1}, {1, 1}}},
    {{{1}}, {{1}, {1, 1}}, {{1}, {1, 1}, {1, 1, 1}}},
    {{{1}}, {{1}, {1, 1}}, {{1}, {1, 1}, {1, 1, 1}},
     {{1}, {1, 1}, {1, 1, 1}, {1, 1, 1, 1}}}}

Rules

  • No standard loopholes as always.
  • The innermost values of your list may be anything (does not need to be consistent for all elements), as long as they are not lists.

This is , so shortest submissions in each language win!


Note: Graphics generated using Mathematica

\$\endgroup\$

marked as duplicate by Erik the Outgolfer code-golf Aug 5 '18 at 0:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ I hammered this as a dupe since the only difference from the other challenge is that \$a=b\$. However, I upvoted this, since it was pretty hard to find the dupe and only managed to do so when I tried solving it, and this is a good challenge otherwise. \$\endgroup\$ – Erik the Outgolfer Aug 5 '18 at 0:12
  • \$\begingroup\$ @EriktheOutgolfer you're right. Huh, I left this post in the Sandbox for 10 days in case this would happen. :( \$\endgroup\$ – JungHwan Min Aug 5 '18 at 0:14
2
\$\begingroup\$

Wolfram Language (Mathematica), 16 bytes

Ha! Of course it is biased towards Mathematica!

Nest[Range,#,#]&

Try it online!

\$\endgroup\$
  • \$\begingroup\$ When you have time, could you explain how this works? (the Listable attribute plays a key role here, and this feature is not common in other languages) \$\endgroup\$ – JungHwan Min Aug 5 '18 at 0:09
  • \$\begingroup\$ Sure, I will try to add an explanation tomorrow. \$\endgroup\$ – Mr. Xcoder Aug 5 '18 at 0:11

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