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

40 Answers 40

11
\$\begingroup\$

APL (Dyalog Unicode), 5 4 bytesSBCS

Anonymous tacit prefix function.

,⍨\⍳

Try it online!

,⍨\ cumulative reverse-concatenation reduction of

 the iota

\$\endgroup\$
  • \$\begingroup\$ "1 being a loose item is allowed" if I understand this, this allows for a 4-byter \$\endgroup\$ – user41805 Feb 11 at 16:16
  • 1
    \$\begingroup\$ @KritixiLithos Ah, that's what it means. Nice. \$\endgroup\$ – Adám Feb 11 at 16:36
7
\$\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
    \$\begingroup\$ Map() 19b, tio.run/##K/r/3zexQCPBKkHH0Ko4OTFPQ1PHUPO/6X8A \$\endgroup\$ – Vlo Feb 11 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 at 20:19
7
\$\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\$
  • 1
    \$\begingroup\$ Reminds me of codegolf.stackexchange.com/a/133116, a Pyth answer consisting only of S and s. \$\endgroup\$ – my pronoun is monicareinstate Feb 11 at 14:53
  • 1
    \$\begingroup\$ A couple more 3 byte a-zA-Z only solutions are RrL and RrE. \$\endgroup\$ – Jonathan Allan Feb 11 at 22:56
  • \$\begingroup\$ @JonathanAllan I don't think I understand the RrE one :/ \$\endgroup\$ – RGS Feb 11 at 23:01
6
\$\begingroup\$

05AB1E, 3 bytes

LLí

Try it online!

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

Pure Bash (no external utilities), 29

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

Try it online!

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

Wolfram Language (Mathematica), 24 bytes

Reverse/@Range/@Range@#&

Try it online!

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

Raku, 13 bytes

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

Try it online!

Cumulative reverse with the range 1 to input.

\$\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\$

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\$
  • \$\begingroup\$ It feels like you are from the previous decade - you are wielding GolfScript so well! \$\endgroup\$ – a'_' Feb 12 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 at 14:08
3
\$\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\$
3
\$\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
    \$\begingroup\$ 49 bytes \$\endgroup\$ – Shaggy Feb 11 at 20:19
3
\$\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\$
3
\$\begingroup\$

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 
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\$
  • \$\begingroup\$ What do you run DOS on? \$\endgroup\$ – S.S. Anne Feb 12 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 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 at 23:13
3
\$\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\$
  • 1
    \$\begingroup\$ 10 bytes. I have no idea why X... doesn't work. \$\endgroup\$ – nwellnhof Feb 13 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 at 4:11
2
\$\begingroup\$

Factor, 45 bytes

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

Try it online!

\$\endgroup\$
2
\$\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\$
  • \$\begingroup\$ ->n,*a{(1..n).map{|x|a=[x]+a}} is 30 bytes \$\endgroup\$ – Value Ink Feb 11 at 21:30
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\$
  • \$\begingroup\$ I think you can remove the space after reverse \$\endgroup\$ – mabel Feb 11 at 21:58
  • \$\begingroup\$ @mabel oh yeah, i had that in my code, just posted an old one, mb \$\endgroup\$ – Matthew Wozniak Feb 11 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 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 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 at 0:16
2
\$\begingroup\$

Haskell, 27 bytes

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

Python 3, 46 bytes

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

Try it online!

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

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

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

Try it online!

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

C (gcc), 54 bytes

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

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ OMG I just wrote this and was looking to see if there was any other c answers before posting (not posting)! :-) \$\endgroup\$ – Noodle9 Feb 11 at 21:02
  • \$\begingroup\$ @Noodle9 I wrote this a long time ago but I made some improvements before writing my answer. \$\endgroup\$ – S.S. Anne Feb 11 at 21:18
  • \$\begingroup\$ 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 :) \$\endgroup\$ – Digital Trauma Feb 12 at 20:00
  • 1
    \$\begingroup\$ @Digital You can shorten yours with division -- I'll get back to it later if you need more details. \$\endgroup\$ – S.S. Anne Feb 12 at 21:29
1
\$\begingroup\$

PHP, 54 bytes

for(;$i++<$argn;)$a[]=str_split($s=$i.$s);print_r($a);

Try it online!

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

J, 11 bytes

[:<@|.\1+i.

Try it online!

         i.    a list 0..n-1
        +      add
       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
\$\endgroup\$
1
\$\begingroup\$

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)
\$\endgroup\$
1
\$\begingroup\$

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!

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

Coffeescript, 29 bytes

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

Try it online!

\$\endgroup\$
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\$

K (ngn/k), 8 bytes

|',\1+!:

Try it online!

-2 thanks to ngn :-)

\$\endgroup\$
  • 1
    \$\begingroup\$ it works without { x} too. just add a : to force the last verb (!) to be monadic: |',\1+!: \$\endgroup\$ – ngn Feb 16 at 5:29
  • \$\begingroup\$ @ngn updated. thanks! \$\endgroup\$ – scrawl Feb 16 at 20:52
0
\$\begingroup\$

Pyth, 5 bytes

_MSMS

Try it online!

\$\endgroup\$

Your Answer

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