22
\$\begingroup\$

I didn't invent this challenge, but I find it very interesting to solve.

For every input number, e.g.:

4

Generate a range from 1 to that number:

[1 2 3 4]

And then, for every item in that list, generate a list from 1 to that number:

[[1] [1 2] [1 2 3] [1 2 3 4]]

Then, reverse every item of that list.

[[1] [2 1] [3 2 1] [4 3 2 1]]

Notes:

  • 1 being a loose item is allowed, since flattening will not matter with this anyway.
  • To preserve the spirit of the challenge, the range has to be 1-indexed.
  • You may flatten the list if your platform doesn't support the concept of nested lists.
\$\endgroup\$
9
  • 9
    \$\begingroup\$ what just happened to the original poster?? \$\endgroup\$
    – RGS
    Feb 11 '20 at 17:21
  • 2
    \$\begingroup\$ @RGS Anonymization. \$\endgroup\$
    – S.S. Anne
    Feb 11 '20 at 18:43
  • 1
    \$\begingroup\$ may we output as a sequence of numbers (1 2 1 3 2 1 4 3 2 1), say, if a platform doesn't have a concept of multi-dimensional array/list? \$\endgroup\$
    – 640KB
    Feb 11 '20 at 20:01
  • \$\begingroup\$ @S.S.Anne in what sense? And what would be a plausible reason? \$\endgroup\$
    – RGS
    Feb 11 '20 at 21:37
  • 4
    \$\begingroup\$ @RGS The user has been disassociated from the question for some reason, probably because they didn't want to take credit for it. It's not been made Community Wiki so the answerers will still get reputation. Or, maybe the user deleted their account right after they posted this (since copying the number into /users/ doesn't work). \$\endgroup\$
    – S.S. Anne
    Feb 11 '20 at 21:38

40 Answers 40

1
2
1
\$\begingroup\$

Burlesque, 9 bytes

riroq<-pa

Try it online!

ri  # Read int
ro  # Range [1,N]
q<- # Boxed reverse
pa  # Operate over ((1), (1 2), (1 2 3),...)

Alternative 9 byter, but this also has an empty list as the first element:

riroiT)<-
\$\endgroup\$
1
\$\begingroup\$

T-SQL, 145 bytes

WITH a(n)as(SELECT number FROM spt_values
WHERE'p'=type)SELECT string_agg(@+1-a.n,' ')FROM
a,a b WHERE a.n<=@ and b.n<a.n
GROUP BY b.n ORDER BY 1

Try it online

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

Stax, 6 bytes

RmRr|u

Run and debug it

\$\endgroup\$
0
\$\begingroup\$

Pyth, 5 bytes

_MSMS

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Japt, 4 bytes

õ!õ1

Try it here

\$\endgroup\$
0
0
\$\begingroup\$

Charcoal, 9 bytes

IEN⮌E⊕ι⊕μ

Try it online! Link is to verbose version of code. Explanation:

  N         Input as a number
 E          Map over implicit 0-indexed range
      ι     Current index
     ⊕      Incremented
    E       Map over implicit 0-indexed range
        μ   Inner index
       ⊕    Incremented
   ⮌        Reversed
I           Cast to string for implicit print

There are other ways of getting the inner range such as InclusiveRange(1, Incremented(i)) and Range(1, Plus(2, i)) for the same byte count.

\$\endgroup\$
0
\$\begingroup\$

Clojure, 36 bytes

#(for[i(range %)](range(inc i)0 -1))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Kotlin, 37 bytes

{(1..it).map{(it downTo 1).toList()}}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Java (JDK), 98 bytes

n->{int a[][]=new int[n][],i=0,j;for(;i<n;)for(a[i]=new int[j=++i];j>0;)a[i-1][--j]=i-j;return a;}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Zsh, 22 bytes

eval echo {{1..$1}..1}

Try it online!

{{1..$1}..1} -> {1..1} {2..1} {3..1} {4..1} ...

eval echo {1..1} {2..1} {3..1} ... -> echo 1 2 1 3 2 1 ...

If the sublists must be delimited, then 25 bytes for , or 26 bytes for newline.

\$\endgroup\$
1
2

Your Answer

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