Reversed Iota's

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.
• what just happened to the original poster?? – RGS Feb 11 at 17:21
• @RGS Anonymization. – S.S. Anne Feb 11 at 18:43
• 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? – 640KB Feb 11 at 20:01
• @S.S.Anne in what sense? And what would be a plausible reason? – RGS Feb 11 at 21:37
• @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). – S.S. Anne Feb 11 at 21:38

APL (Dyalog Unicode), 5 4 bytesSBCS

Anonymous tacit prefix function.

,⍨\⍳


Try it online!

,⍨\ cumulative reverse-concatenation reduction of

⍳ the iota

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

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!

• Map() 19b, tio.run/##K/r/3zexQCPBKkHH0Ko4OTFPQ1PHUPO/6X8A – Vlo Feb 11 at 19:39
• @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! – Giuseppe Feb 11 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

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

05AB1E, 3 bytes

LLí


Try it online!

eval eval echo \\{{1..$1}..1}  Try it online! Wolfram Language (Mathematica), 24 bytes Reverse/@Range/@Range@#&  Try it online! 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! • 49 bytes – Shaggy Feb 11 at 20:19 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:

for(;$x<$argn;print_r(range(++$x,1)));  Try it online! Raku, 13 bytes {[\R,] 1..$_}


Try it online!

Cumulative reverse with the range 1 to input.

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). 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! • It feels like you are from the previous decade - you are wielding GolfScript so well! – user92069 Feb 12 at 13:54 • 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. – Mathgeek Feb 12 at 14:08 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.

Ruby, 34 30 bytes

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


Try it online!

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

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

x86-16 machine code, 12 bytes

Binary:

00000000: 33c0 4050 ab48 75fc 58e2 f7c3         3.@P.Hu.X...


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


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

Example I/O using DOS test driver program:

• What do you run DOS on? – S.S. Anne Feb 12 at 23:06
• @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. – 640KB Feb 12 at 23:11
• Ah, DOSBox. Now that I look at it more closely it does look like DOSBox. – S.S. Anne Feb 12 at 23:13

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)

• 10 bytes. I have no idea why X... doesn't work. – nwellnhof Feb 13 at 1:38
• @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 >> – SirBogman Feb 13 at 4:11

Factor, 45 bytes

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


Try it online!

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

Try it online!

-22 bytes thanks to @79037662

• I think you can remove the space after reverse – mabel Feb 11 at 21:58
• @mabel oh yeah, i had that in my code, just posted an old one, mb – Matthew Wozniak Feb 11 at 21:59
• You can remove the type annotation for main. Also, you could write a function instead of a full program. – SirBogman Feb 11 at 22:27
• 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. – 79037662 Feb 12 at 0:15
• 26 bytes if it's just a function and not a whole program: tio.run/##y0gszk7Nyfn/… – 79037662 Feb 12 at 0:16

f n=scanl(flip(:))[1][2..n]


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.

K (ngn/k), 8 bytes

|',\1+!:


Try it online!

-2 thanks to ngn :-)

• @ngn updated. thanks! – scrawl Feb 16 at 20:52

Python 3, 46 bytes

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


Try it online!

Python 3, 69 $$\\cdots\$$ 48 46 bytes

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


Try it online!

C (gcc), 54 bytes

i,t;f(n){for(i=0;n/++i;)for(t=i;t;)printf("%d ",t--);}


Try it online!

• OMG I just wrote this and was looking to see if there was any other c answers before posting (not posting)! :-) – Noodle9 Feb 11 at 21:02
• @Noodle9 I wrote this a long time ago but I made some improvements before writing my answer. – S.S. Anne Feb 11 at 21:18
• You can get this shorter if you use just one loop - here's a start. But I got it even shorter with recursive, non-loop approach :) – Digital Trauma Feb 12 at 20:00
• @Digital You can shorten yours with division -- I'll get back to it later if you need more details. – S.S. Anne Feb 12 at 21:29

for(;$i++<$argn;)$a[]=str_split($s=$i.$s);print_r($a);  Try it online! Retina, 20 bytes .+ * L$w_+
$.& O$^


Try it online! Explanation:

.+
*


Convert the input to a string of _s of that length.

L$w_+$.&


List the lengths of all the possible substrings. The way Retina enumerates substrings means that the lengths are in the exact reverse of the desired order.

O\$^


Reverse the output line-by-line.

J, 11 bytes

[:<@|.\1+i.


Try it online!

         i.    a list 0..n-1
1       one to it -> a list 1..n
\        for each prefix of the list (1, 1 2, 1 2 3...)
|.         reverse
@           and
<            box
[:             cap the fork


K (oK), 14 bytes

{{|1+!x}'1+!x}


Try it online!

{            }  a function with parameter x
!x   a list 0..n-1
1+     add one to it -> a list 1..n
{     }'       apply this function to each of the elements of the above list
!x         a list 0..n-1
1+           add one to it -> a list 1..n
|             reverse


MathGolf, 4 bytes

╒╒mx


Try it online.

Explanation:

╒     # Push a list in the range [1, (implicit) input-integer]
╒    # Convert each inner value to a list in the range [1, value]
m   # Map over this list of lists:
x  #  And reverse each inner list
# (after which the entire stack joined together is output implicitly as result)


C (gcc), 42

I felt like there must be an elegant recursive solution to this. This is the shortest I could come up with, though there is probably more room for golf.

• 4 bytes saved thanks to @S.S.Anne.
f(n,m){n&&f(n-m/n,m%n+1)*printf("%d ",m);}


Initially, the function is called as f(N, 1), where N is the input integer.

Try it online!

• f(n-m/n saves 3 bytes. Test it with higher inputs, though. – S.S. Anne Feb 12 at 21:54
• @S.S.Anne Thanks. I like the symmetry of using m/n and m%n as well :). – Digital Trauma Feb 12 at 21:59
• In general any >= can be replaced by /. The same goes for <= but you reverse the arguments. – S.S. Anne Feb 12 at 22:08
• 42 bytes – S.S. Anne Feb 13 at 0:46

Coffeescript, 29 bytes

r=(x)->[[y..1]for y in[1..x]]


Try it online!

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)<-