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

63 Answers 63

1
2 3
11
\$\begingroup\$

APL (Dyalog Unicode), (削除) 5 (削除ここまで) 4 bytes SBCS

Anonymous tacit prefix function.

,⍨\⍳

Try it online!

,⍨\ cumulative reverse-concatenation reduction of

the iota

answered Feb 11, 2020 at 14:44
\$\endgroup\$
2
  • \$\begingroup\$ "1 being a loose item is allowed" if I understand this, this allows for a 4-byter \$\endgroup\$ Commented Feb 11, 2020 at 16:16
  • 1
    \$\begingroup\$ @KritixiLithos Ah, that's what it means. Nice. \$\endgroup\$ Commented 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!

answered Feb 11, 2020 at 16:47
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Map() 19b, tio.run/##K/r/3zexQCPBKkHH0Ko4OTFPQ1PHUPO/6X8A \$\endgroup\$ Commented 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\$ Commented Feb 11, 2020 at 20:19
8
\$\begingroup\$

Pure Bash (no external utilities), 29

eval eval echo \\{{1..1ドル}..1}

Try it online!

answered Feb 11, 2020 at 16:13
\$\endgroup\$
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

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

05AB1E, 3 bytes

LLí

Try it online!

answered Feb 11, 2020 at 16:15
\$\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.

answered Feb 11, 2020 at 16:49
\$\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)
answered Feb 11, 2020 at 21:44
\$\endgroup\$
3
  • 1
    \$\begingroup\$ 10 bytes. I have no idea why X... doesn't work. \$\endgroup\$ Commented 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\$ Commented 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\$ Commented Feb 2, 2023 at 0:31
5
\$\begingroup\$

K (ngn/k), 8 bytes

|',1円+!:

Try it online!

-2 thanks to ngn :-)

answered Feb 12, 2020 at 15:44
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Another 8-byter is -!'-1-!: \$\endgroup\$ Commented Jan 29, 2023 at 20:05
  • \$\begingroup\$ @coltim neat :-) \$\endgroup\$ Commented Jan 29, 2023 at 22:12
  • \$\begingroup\$ Based on coltims suggestion, -!'|!-: works for 7 \$\endgroup\$ Commented Jun 14, 2023 at 8:10
5
\$\begingroup\$

Wolfram Language (Mathematica), 20 bytes

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

Try it online!

-3 bytes from @att

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

Python 3, 46 bytes

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

Try it online!

answered Feb 11, 2020 at 15:00
\$\endgroup\$
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!

answered Feb 11, 2020 at 15:08
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 49 bytes \$\endgroup\$ Commented 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!

answered Feb 11, 2020 at 16:39
\$\endgroup\$
4
\$\begingroup\$

Raku, 13 bytes

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

Try it online!

Cumulative reverse with the range 1 to input.

answered Feb 11, 2020 at 22:52
\$\endgroup\$
1
  • \$\begingroup\$ One of the great things about Raku is that there are at least two equally short ways to do it. \$\endgroup\$ Commented Feb 12, 2020 at 13:30
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

answered Feb 11, 2020 at 22:46
\$\endgroup\$
3
  • \$\begingroup\$ What do you run DOS on? \$\endgroup\$ Commented 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\$ Commented Feb 12, 2020 at 23:11
  • \$\begingroup\$ Ah, DOSBox. Now that I look at it more closely it does look like DOSBox. \$\endgroup\$ Commented 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.
answered Jan 29, 2023 at 15:58
\$\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).

answered Feb 11, 2020 at 14:41
\$\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!

answered Feb 11, 2020 at 15:33
\$\endgroup\$
2
  • \$\begingroup\$ It feels like you are from the previous decade - you are wielding GolfScript so well! \$\endgroup\$ Commented 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\$ Commented Feb 12, 2020 at 14:08
3
\$\begingroup\$

Factor, 45 bytes

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

Try it online!

answered Feb 11, 2020 at 19:00
\$\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.

answered Feb 11, 2020 at 15:33
\$\endgroup\$
1
  • \$\begingroup\$ ->n,*a{(1..n).map{|x|a=[x]+a}} is 30 bytes \$\endgroup\$ Commented 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.

answered Feb 13, 2020 at 15:39
\$\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.

answered Feb 13, 2020 at 20:52
\$\endgroup\$
3
\$\begingroup\$

Elm, 45 bytes

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

Try it on Ellie

answered Jan 29, 2023 at 16:54
\$\endgroup\$
3
\$\begingroup\$

Arturo, (削除) 22 (削除ここまで) 18 bytes

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

Try it

answered Jan 29, 2023 at 12:37
\$\endgroup\$
2
\$\begingroup\$

Brachylog, 5 bytes

⟦1⟧1m

Try it online!

Explanation

⟦1 Ascending range from 1 to the input
 m Map:
 ⟧1 Descending range from 1 to the mapped element
answered Feb 12, 2020 at 10:09
\$\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\$ Commented Feb 11, 2020 at 21:58
  • \$\begingroup\$ @mabel oh yeah, i had that in my code, just posted an old one, mb \$\endgroup\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented 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)<-
answered Feb 13, 2020 at 20:47
\$\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!

answered Jan 29, 2023 at 19:39
\$\endgroup\$
2
\$\begingroup\$

shell with seq loop, 46 bytes

for n in `seq 1ドル`;do seq -s\ -t'\n' $n 1;done
answered Jan 29, 2023 at 20:45
\$\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)));

answered Jan 29, 2023 at 23:41
\$\endgroup\$
1
  • 1
    \$\begingroup\$ +1 for avoiding recursion. Welcome CG.SE EM \$\endgroup\$ Commented Jan 30, 2023 at 3:44
1
2 3

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.