28
\$\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
  • 10
    \$\begingroup\$ what just happened to the original poster?? \$\endgroup\$
    – RGS
    Feb 11, 2020 at 17:21
  • 2
    \$\begingroup\$ @RGS Anonymization. \$\endgroup\$
    – S.S. Anne
    Feb 11, 2020 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, 2020 at 20:01
  • \$\begingroup\$ @S.S.Anne in what sense? And what would be a plausible reason? \$\endgroup\$
    – RGS
    Feb 11, 2020 at 21:37
  • 5
    \$\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, 2020 at 21:38

58 Answers 58

11
\$\begingroup\$

APL (Dyalog Unicode), 5 4 bytesSBCS

Anonymous tacit prefix function.

,⍨\⍳

Try it online!

,⍨\ cumulative reverse-concatenation reduction of

 the iota

\$\endgroup\$
2
  • \$\begingroup\$ "1 being a loose item is allowed" if I understand this, this allows for a 4-byter \$\endgroup\$
    – user41805
    Feb 11, 2020 at 16:16
  • 1
    \$\begingroup\$ @KritixiLithos Ah, that's what it means. Nice. \$\endgroup\$
    – Adám
    Feb 11, 2020 at 16:36
9
\$\begingroup\$

R, 22 19 bytes

Map(`:`,1:scan(),1)

Try it online!

Map(f,...) applies f elementwise to each member of ..., recycling as needed, resulting in a list, so we just supply 1 as the to argument to : to get the reversed.

-3 bytes thanks to Vlo!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Map() 19b, tio.run/##K/r/3zexQCPBKkHH0Ko4OTFPQ1PHUPO/6X8A \$\endgroup\$
    – Vlo
    Feb 11, 2020 at 19:39
  • 1
    \$\begingroup\$ @Vlo I don't know why I didn't think of that; Map is pretty much always shorter than lapply, and more flexible too...Thanks! \$\endgroup\$
    – Giuseppe
    Feb 11, 2020 at 20:19
8
\$\begingroup\$

Jelly, 3 bytes

RRU
R     Range from 1 to input
 R    (implicitly) map over said range and create a range from 1 to this element
  U   reverse each of those

You can try it online!

Courtesy of @JonathanAllan, we also have two other 3-"byters"

RrL
RrE

I think it is not often that golfing languages solve a challenge with only a-zA-Z characters

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Reminds me of codegolf.stackexchange.com/a/133116, a Pyth answer consisting only of S and s. \$\endgroup\$ Feb 11, 2020 at 14:53
  • 1
    \$\begingroup\$ A couple more 3 byte a-zA-Z only solutions are RrL and RrE. \$\endgroup\$ Feb 11, 2020 at 22:56
  • \$\begingroup\$ @JonathanAllan I don't think I understand the RrE one :/ \$\endgroup\$
    – RGS
    Feb 11, 2020 at 23:01
7
\$\begingroup\$

Pure Bash (no external utilities), 29

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

Try it online!

\$\endgroup\$
6
\$\begingroup\$

05AB1E, 3 bytes

LLí

Try it online!

\$\endgroup\$
5
\$\begingroup\$

shell + sed, 15 bytes

seq $1|sed G\;h

Try it online!

Outputs like so, 1\n\n2\n1\n\n3\n2\n1\n\n[...]

seq $1 creates a sequence from 1 to the first argument $1

|sed ... which is piped into a sed script

sed works on a line-by-line basis; it first reads the first line into the buffer, called the "pattern space", after which the program commands is run on it. At the end of the program's execution on the first line, the remaining pattern space is implicitly printed. Then sed reads the next line into the pattern space, replacing the previous contents, and runs the commands on it, repeating for all lines of input (unless a command specifies otherwise).

The pattern space is not saved between lines, but what is is the hold space. The hold space is another buffer, that starts empty, and can be modified by program commands. Its contents are carried on to the execution of the next line of input.

The G command appends a newline followed by the content of the hold space to that of the pattern space. Then the h command replaces the hold space with the content of the pattern space. This effectively reverses the lines of input encountered so far, writing them to the pattern space – implicitly printing at the end of processing the current line – and saving them to the hold space so that upon reading subsequent lines of input, the new reversed "list" can be constructed with G;h.

The ; is escaped in the program as \; because otherwise the shell interprets it as terminating a shell command.

\$\endgroup\$
5
\$\begingroup\$

Perl 6 Raku, 24 15 13 10 bytes

-2 removed parenthesis

-3 thanks to nwellnhof

^*+1 X…1

Try it online!

Explanation

^*+1     # make a range from 1 .. argument (whatever star).
     X…1 # create ranges descending to 1 using cross product metaoperator.

Previous version, 24 bytes

(^<<(^*+1)X+1)>>.reverse

Try it online!

Explanation

     ^*+1                # make a range from 1 .. argument (whatever star)
 ^<<(    )               # replace each element with a range from 0 .. element - 1
                         # (via hyper prefix operator)
          X+1            # shift the range to 1 .. element
                         # (via cross product metaoperator)
(            )>>.reverse # reverse each list (via hyper method operator)
\$\endgroup\$
3
  • 1
    \$\begingroup\$ 10 bytes. I have no idea why X... doesn't work. \$\endgroup\$
    – nwellnhof
    Feb 13, 2020 at 1:38
  • 1
    \$\begingroup\$ @nwellnhof Very interesting. I think I tried X..., but not this. I remember getting the same results. I definitely use X much more frequently than a pair of >> \$\endgroup\$
    – SirBogman
    Feb 13, 2020 at 4:11
  • \$\begingroup\$ Some update has made the unicode operator act the same as the ascii equivalent, breaking this elegant answer \$\endgroup\$
    – Jo King
    Feb 2 at 0:31
5
\$\begingroup\$

K (ngn/k), 8 bytes

|',\1+!:

Try it online!

-2 thanks to ngn :-)

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Another 8-byter is -!'-1-!: \$\endgroup\$
    – coltim
    Jan 29 at 20:05
  • \$\begingroup\$ @coltim neat :-) \$\endgroup\$
    – scrawl
    Jan 29 at 22:12
  • \$\begingroup\$ Based on coltims suggestion, -!'|!-: works for 7 \$\endgroup\$
    – ovs
    Jun 14 at 8:10
5
\$\begingroup\$

Wolfram Language (Mathematica), 20 bytes

Range[Range@#,1,-1]&

Try it online!

-3 bytes from @att

\$\endgroup\$
3
  • \$\begingroup\$ Range is Listable, so you can save one byte: Reverse/@Range@Range@#&. \$\endgroup\$
    – alephalpha
    Jan 30 at 7:14
  • \$\begingroup\$ #-Range@#+1&~Array~#& for 21 \$\endgroup\$
    – att
    Jun 14 at 6:19
  • \$\begingroup\$ ...and Range[Range@#,1,-1]& for 20 \$\endgroup\$
    – att
    Jun 14 at 6:20
4
\$\begingroup\$

JavaScript (ES6),  50  49 bytes

Saved 1 byte thanks to @Shaggy

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

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 49 bytes \$\endgroup\$
    – Shaggy
    Feb 11, 2020 at 20:19
4
\$\begingroup\$

PHP, 54 bytes

function($x){for(;$y<$x;$a[]=range(++$y,1));return$a;}

Try it online!

Or recursive:

PHP, 54 bytes

function f($x){return$x?f($x-1)+[$x=>range($x,1)]:[];}

Try it online!

Or with PHP-formatted printed output:

PHP, 38 bytes

for(;$x<$argn;print_r(range(++$x,1)));

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Raku, 13 bytes

{[\R,] 1..$_}

Try it online!

Cumulative reverse with the range 1 to input.

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

x86-16 machine code, 12 bytes

Binary:

00000000: 33c0 4050 ab48 75fc 58e2 f7c3         [email protected]...

Unassembled listing:

33 C0       XOR  AX, AX         ; AX = 0
        OUT_LOOP:
40          INC  AX             ; start at 1
50          PUSH AX             ; save starting position
        IN_LOOP:
AB          STOSW               ; write to output buffer, increment DI
48          DEC  AX             ; AX--
75 FC       JNZ  IN_LOOP        ; if AX > 0, keep looping
58          POP  AX             ; restore starting position
E2 F7       LOOP OUT_LOOP 
C3          RET                 ; return to caller

Input Number in CX, output array of WORD, at [DI].

Example I/O using DOS test driver program:

enter image description here

\$\endgroup\$
3
  • \$\begingroup\$ What do you run DOS on? \$\endgroup\$
    – S.S. Anne
    Feb 12, 2020 at 23:06
  • \$\begingroup\$ @S.S.Anne this example I just ran in DOSBox because I was lazy. Typically I'll use PCE to test since it's an actual IBM PC 5150 "bare metal" emulation. \$\endgroup\$
    – 640KB
    Feb 12, 2020 at 23:11
  • \$\begingroup\$ Ah, DOSBox. Now that I look at it more closely it does look like DOSBox. \$\endgroup\$
    – S.S. Anne
    Feb 12, 2020 at 23:13
4
\$\begingroup\$

BQN, 10 bytes

Anonymous tacit prefix function.

(⌽1+↕)¨1+↕

Try it online!

Explanation

(⌽1+↕)¨1+↕
       1+↕    1.  Get list of integers from 1 to n.
      ¨       2a. For each integer i in the list...
( 1+↕)        2b. get a list of integers from 1 to i...
(⌽   )        2c. and then reverse that list.
\$\endgroup\$
3
\$\begingroup\$

PowerShell, 19 bytes

1.."$args"|%{$_..1}

Try it online!

Generates the range from 1 to input $args, then constructs the reversed range for each of those numbers. Tack on a -join to better see how the arrays are created (because PowerShell inserts a newline between each element, it's tough to see the individual arrays).

\$\endgroup\$
3
\$\begingroup\$

Python 3, 46 bytes

lambda n:[[*range(i+1,0,-1)]for i in range(n)]

Try it online!

\$\endgroup\$
3
\$\begingroup\$

GolfScript, 12 (13) bytes

,{)),(;-1%}%`
,{)),(;-1%}%` #Reversed iota of iota
,             #0 to n-1 iota
 {        }%  #For each element in the iota
 {))      }   #Increment by 2
 {  ,     }   #Iota
 {   (;   }   #Pop leading 0
 {     -1%}   #Reverse it
            ` #Pretty output, not needed if you use a better stack-interpreter

Try it online!

Below is my old solution, I gained inspiration after posting and improved it.

),(;{),(;-1%}%`

Comma is the function that builds an array 0 to n-1, "iota expand".

),(;{),(;-1%}%` #Take in a number, output reversed expanded iota
)               #Increment input by 1
 ,              #Iota expand
  (;            #Remove leading 0
    {       }%  #For every element, do the following
    {)      }   #Increment by 1
    { ,     }   #Iota expand
    {  (;   }   #Remove leading 0
    {    -1%}   #Reverse
              ` #Pretty output; technically not needed

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ It feels like you are from the previous decade - you are wielding GolfScript so well! \$\endgroup\$
    – user92069
    Feb 12, 2020 at 13:54
  • 1
    \$\begingroup\$ I had a week off and wanted to learn an esolang for codegolf - osabie was a bit too over-the-top, APL had too much to remember, but Golfscript was easy for me to understand right away - so I spent an entire week just doubling down learning the ins and outs, and now I can visualize the stack and understand exactly what I'm doing as I'm doing it, it's a very elegant language. \$\endgroup\$
    – Mathgeek
    Feb 12, 2020 at 14:08
3
\$\begingroup\$

Factor, 45 bytes

: f ( n -- s ) [1,b] [ 1 [a,b] >array ] map ;

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Ruby, 34 30 bytes

->n,*a{(1..n).map{|x|a=[x]+a}}

Try it online!

Thanks Value Ink (as usual) for -4 bytes.

\$\endgroup\$
1
  • \$\begingroup\$ ->n,*a{(1..n).map{|x|a=[x]+a}} is 30 bytes \$\endgroup\$
    – Value Ink
    Feb 11, 2020 at 21:30
3
\$\begingroup\$

Haskell, 27 bytes

f n=scanl(flip(:))[1][2..n]
\$\endgroup\$
3
\$\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\$
3
\$\begingroup\$

Fortran (GFortran), 47 bytes

read*,i
print*,("{",(j,j=k,1,-1),"}",k=1,i)
end

Try it online!

Could remove 8 chars by getting rid of printed brackets if anyone would believe that they are lists otherwise.

\$\endgroup\$
3
\$\begingroup\$

Elm, 45 bytes

r=List.range 1
f=List.map(List.reverse<<r)<<r

Try it on Ellie

\$\endgroup\$
3
\$\begingroup\$

Arturo, 22 18 bytes

$=>[map&=>[@&..1]]

Try it

\$\endgroup\$
2
\$\begingroup\$

Brachylog, 5 bytes

⟦₁⟧₁ᵐ

Try it online!

Explanation

⟦₁     Ascending range from 1 to the input
    ᵐ  Map:
  ⟧₁     Descending range from 1 to the mapped element
\$\endgroup\$
0
2
\$\begingroup\$

Haskell, 74 73 51 bytes

main=do i<-getLine;print[[x,x-1..1]|x<-[1..read i]]

Try it online!

-22 bytes thanks to @79037662

\$\endgroup\$
6
  • \$\begingroup\$ I think you can remove the space after reverse \$\endgroup\$
    – mabel
    Feb 11, 2020 at 21:58
  • \$\begingroup\$ @mabel oh yeah, i had that in my code, just posted an old one, mb \$\endgroup\$ Feb 11, 2020 at 21:59
  • 1
    \$\begingroup\$ You can remove the type annotation for main. Also, you could write a function instead of a full program. \$\endgroup\$
    – SirBogman
    Feb 11, 2020 at 22:27
  • \$\begingroup\$ 51 bytes: main=do i<-getLine;print[[x,x-1..1]|x<-[1..read i]] Removing unnecessary boilerplate, and using [x,x-1..1] instead of reverse. \$\endgroup\$
    – 79037662
    Feb 12, 2020 at 0:15
  • 1
    \$\begingroup\$ 26 bytes if it's just a function and not a whole program: tio.run/##y0gszk7Nyfn/… \$\endgroup\$
    – 79037662
    Feb 12, 2020 at 0:16
2
\$\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\$
2
\$\begingroup\$

Go, 105 bytes

func(n int)(o[][]int){for i:=1;i<=n;i++{J:=[]int{}
for j:=i;j>0;j--{J=append(J,j)}
o=append(o,J)}
return}

Attempt This Online!

\$\endgroup\$
2
\$\begingroup\$

shell with seq loop, 46 bytes

for n in `seq $1`;do seq -s\  -t'\n' $n 1;done
\$\endgroup\$
2
\$\begingroup\$

JavaScript, 60 bytes

Without recursion:

n=>eval('for(a=[];n;a[--n]=b)for(b=[j=n];j-->1;b[n-j]=j);a')

Try it:

f=n=>eval('for(a=[];n;a[--n]=b)for(b=[j=n];j-->1;b[n-j]=j);a')

console.log(JSON.stringify(f(1)));
console.log(JSON.stringify(f(2)));
console.log(JSON.stringify(f(3)));
console.log(JSON.stringify(f(4)));

\$\endgroup\$
1
  • 1
    \$\begingroup\$ +1 for avoiding recursion. Welcome CG.SE EM \$\endgroup\$
    – gildux
    Jan 30 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.