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.
63 Answers 63
APL (Dyalog Unicode), (削除) 5 (削除ここまで) 4 bytes SBCS
Anonymous tacit prefix function.
,⍨\⍳
,⍨\ cumulative reverse-concatenation reduction of
⍳ the iota
-
\$\begingroup\$ "1 being a loose item is allowed" if I understand this, this allows for a 4-byter \$\endgroup\$user41805– user418052020年02月11日 16:16:36 +00:00Commented Feb 11, 2020 at 16:16
-
1\$\begingroup\$ @KritixiLithos Ah, that's what it means. Nice. \$\endgroup\$Adám– Adám2020年02月11日 16:36:03 +00:00Commented Feb 11, 2020 at 16:36
R, (削除) 22 (削除ここまで) 19 bytes
Map(`:`,1:scan(),1)
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!
-
2\$\begingroup\$
Map()19b, tio.run/##K/r/3zexQCPBKkHH0Ko4OTFPQ1PHUPO/6X8A \$\endgroup\$Vlo– Vlo2020年02月11日 19:39:06 +00:00Commented Feb 11, 2020 at 19:39 -
1\$\begingroup\$ @Vlo I don't know why I didn't think of that;
Mapis pretty much always shorter thanlapply, and more flexible too...Thanks! \$\endgroup\$Giuseppe– Giuseppe2020年02月11日 20:19:49 +00:00Commented Feb 11, 2020 at 20:19
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
-
1\$\begingroup\$ Reminds me of codegolf.stackexchange.com/a/133116, a Pyth answer consisting only of
Sands. \$\endgroup\$the default.– the default.2020年02月11日 14:53:01 +00:00Commented Feb 11, 2020 at 14:53 -
1\$\begingroup\$ A couple more 3 byte
a-zA-Zonly solutions areRrLandRrE. \$\endgroup\$Jonathan Allan– Jonathan Allan2020年02月11日 22:56:41 +00:00Commented Feb 11, 2020 at 22:56 -
\$\begingroup\$ @JonathanAllan I don't think I understand the
RrEone :/ \$\endgroup\$RGS– RGS2020年02月11日 23:01:13 +00:00Commented Feb 11, 2020 at 23:01
shell + sed, 15 bytes
seq 1ドル|sed G\;h
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.
(削除) Perl 6 (削除ここまで) Raku, (削除) 24 15 13 (削除ここまで) 10 bytes
-2 removed parenthesis
-3 thanks to nwellnhof
^*+1 X...1
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
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)
-
1
-
1\$\begingroup\$ @nwellnhof Very interesting. I think I tried
X..., but not this. I remember getting the same results. I definitely useXmuch more frequently than a pair of>>\$\endgroup\$SirBogman– SirBogman2020年02月13日 04:11:43 +00:00Commented 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– Jo King2023年02月02日 00:31:21 +00:00Commented Feb 2, 2023 at 0:31
-
\$\begingroup\$
RangeisListable, so you can save one byte:Reverse/@Range@Range@#&. \$\endgroup\$alephalpha– alephalpha2023年01月30日 07:14:14 +00:00Commented Jan 30, 2023 at 7:14 -
\$\begingroup\$
#-Range@#+1&~Array~#&for 21 \$\endgroup\$att– att2023年06月14日 06:19:25 +00:00Commented Jun 14, 2023 at 6:19 -
\$\begingroup\$ ...and
Range[Range@#,1,-1]&for 20 \$\endgroup\$att– att2023年06月14日 06:20:11 +00:00Commented Jun 14, 2023 at 6:20
JavaScript (ES6), (削除) 50 (削除ここまで) 49 bytes
Saved 1 byte thanks to @Shaggy
f=n=>n?[...f(n-1),(g=_=>n?[n,...g(--n)]:[])()]:[]
-
1
PHP, 54 bytes
function($x){for(;$y<$x;$a[]=range(++$y,1));return$a;}
Or recursive:
PHP, 54 bytes
function f($x){return$x?f($x-1)+[$x=>range($x,1)]:[];}
Or with PHP-formatted printed output:
PHP, 38 bytes
for(;$x<$argn;print_r(range(++$x,1)));
-
\$\begingroup\$ One of the great things about Raku is that there are at least two equally short ways to do it. \$\endgroup\$SirBogman– SirBogman2020年02月12日 13:30:20 +00:00Commented Feb 12, 2020 at 13:30
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:
-
\$\begingroup\$ What do you run DOS on? \$\endgroup\$S.S. Anne– S.S. Anne2020年02月12日 23:06:17 +00:00Commented Feb 12, 2020 at 23:06
-
-
\$\begingroup\$ Ah, DOSBox. Now that I look at it more closely it does look like DOSBox. \$\endgroup\$S.S. Anne– S.S. Anne2020年02月12日 23:13:14 +00:00Commented Feb 12, 2020 at 23:13
BQN, 10 bytes
Anonymous tacit prefix function.
(⌽1+↕) ̈1+↕
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.
PowerShell, 19 bytes
1.."$args"|%{$_..1}
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).
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
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
-
\$\begingroup\$ It feels like you are from the previous decade - you are wielding GolfScript so well! \$\endgroup\$user92069– user920692020年02月12日 13:54:28 +00:00Commented 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– Mathgeek2020年02月12日 14:08:06 +00:00Commented Feb 12, 2020 at 14:08
Ruby, (削除) 34 (削除ここまで) 30 bytes
->n,*a{(1..n).map{|x|a=[x]+a}}
Thanks Value Ink (as usual) for -4 bytes.
-
\$\begingroup\$
->n,*a{(1..n).map{|x|a=[x]+a}}is 30 bytes \$\endgroup\$Value Ink– Value Ink2020年02月11日 21:30:14 +00:00Commented Feb 11, 2020 at 21:30
Zsh, 22 bytes
eval echo {{1..1ドル}..1}
{{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.
Fortran (GFortran), 47 bytes
read*,i
print*,("{",(j,j=k,1,-1),"}",k=1,i)
end
Could remove 8 chars by getting rid of printed brackets if anyone would believe that they are lists otherwise.
Haskell, (削除) 74 (削除ここまで) (削除) 73 (削除ここまで) 51 bytes
main=do i<-getLine;print[[x,x-1..1]|x<-[1..read i]]
-22 bytes thanks to @79037662
-
\$\begingroup\$ I think you can remove the space after
reverse\$\endgroup\$mabel– mabel2020年02月11日 21:58:02 +00:00Commented Feb 11, 2020 at 21:58 -
\$\begingroup\$ @mabel oh yeah, i had that in my code, just posted an old one, mb \$\endgroup\$Matthew Wozniak– Matthew Wozniak2020年02月11日 21:59:51 +00:00Commented 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– SirBogman2020年02月11日 22:27:33 +00:00Commented 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 ofreverse. \$\endgroup\$79037662– 790376622020年02月12日 00:15:01 +00:00Commented 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– 790376622020年02月12日 00:16:49 +00:00Commented Feb 12, 2020 at 0:16
Burlesque, 9 bytes
riroq<-pa
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)<-
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}
shell with seq loop, 46 bytes
for n in `seq 1ドル`;do seq -s\ -t'\n' $n 1;done
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)));
-
1\$\begingroup\$ +1 for avoiding recursion. Welcome CG.SE EM \$\endgroup\$gildux– gildux2023年01月30日 03:44:40 +00:00Commented Jan 30, 2023 at 3:44
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\$