19
\$\begingroup\$

Related: Let's design a digit mosaic, Print/Output the L-phabet. Sandbox post here

Given 2 inputs C = columns and rows, S = starting point output a matrix as follow:

Input 4, 3

1   2   3   0
2   2   3   0
3   3   3   0
0   0   0   0

Explanation

Given C = 4, S = 3

1) Create a C x C matrix filled with 0

         4 columns
4     _____|____
     |          |
r  --0  0   0   0
o |  0  0   0   0
w |  0  0   0   0
s  --0  0   0   0

2) Fill with S values within row and column S, then subtract 1 from S and repeat until S = 0. This case S = 3

             Column 3 
S = 3           |
                v
        0   0   3   0
        0   0   3   0
Row 3-->3   3   3   0
        0   0   0   0


         Column 2
S = 2       |
            v
        0   2   3   0
Row 2-->2   2   3   0
        3   3   3   0
        0   0   0   0


     Column 1
S=1     |
        v
Row 1-->1   2   3   0
        2   2   3   0
        3   3   3   0
        0   0   0   0



Final Result

1   2   3   0
2   2   3   0
3   3   3   0
0   0   0   0

Rules

  • Assume C >= S >= 0
  • The output can be a matrix, list of lists, array (1-dimensional or 2-dimensional) etc.
  • You can take inputs via any default I/O format
  • Your program, function, etc... may be 1-indexing or 0-indexing. Please specify which one is.

Note Explanation is 1-indexing


Winning criteria

\$\endgroup\$
0

21 Answers 21

6
\$\begingroup\$

Jelly, 8 bytes

»>⁴¬×»µþ

Try it online!

How it works

Jelly's Outer Product Atom (þ)

You can think of Jelly's outer product atom, þ, as a quick (operator) that, given integer arguments \$X\$ and \$Y\$ (in this case \$X=Y=\text{first argument }\$), produces the following matrix of tuples:

$$\left[\begin{matrix} (1, 1) & (2, 1) & (3, 1) & \cdots & (X, 1) \\ (1, 2) & (2, 2) & (3, 2) & \cdots & (X, 2) \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ (1, Y) & (2, Y) & (3, Y) & \cdots & (X, Y) \end{matrix}\right]$$

It also applies the link right before it to all pairs, let's call it \$\:f\$, which behaves like a function which takes two arguments, producing something like this:

$$\left[\begin{matrix} f(1, 1) & f(2, 1) & f(3, 1) & \cdots & f(X, 1) \\ f(1, 2) & f(2, 2) & f(3, 2) & \cdots & f(X, 2) \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ f(1, Y) & f(2, Y) & f(3, Y) & \cdots & f(X, Y) \end{matrix}\right]$$

How is it relevant to the task at hand?

This works by noticing that every value in the expected output is just a table of maximal indices, or \$0\$ if this maximum exceeds our second argument. Therefore, we can create the following link to perform this mapping:

»>⁴¬×» – Dyadic (2-argument) link.
»      – Maximum of the X, Y coordinates.
 >⁴    – Check if this exceeds the second argument of the program.
   ¬   – Negate this boolean.
    ×» – And multiply by the maximum, computed again.
\$\endgroup\$
6
\$\begingroup\$

R, 47 41 bytes

function(C,S,m=outer(1:C,1:C,pmax))m*!m>S

Try it online!

1-indexed. Generates the outputs for S==C (no zeros) then zeroes cells which have a value >S using matrix multiplication (thanks Giuseppe for 4 bytes!).

\$\endgroup\$
2
  • \$\begingroup\$ Neat! multiplication will get you some good mileage: 43 bytes \$\endgroup\$
    – Giuseppe
    Commented Jul 19, 2018 at 15:50
  • \$\begingroup\$ @Giuseppe tx! I was able to save two more :) \$\endgroup\$
    – JayCe
    Commented Jul 19, 2018 at 15:55
5
\$\begingroup\$

Octave, 31 bytes

@(C,S)(u=max(t=1:C,t')).*(u<=S)

Anonymous function that returns a matrix. Uses 1-based indexing.

Try it online!

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

Haskell, 47 45 bytes

-2 bytes by changing the output format to one-dimensional list.

c&s|x<-[1..c]=[sum[j|j<=s]|j<-x>>=(<$>x).max]

Try it online!

Explanation

The term x >>= (<$> x) . max is a golfed version of

concat [ max i <$> x | i <- x ]

which evaluates to [1,2,3,4..c, 2,2,3,4..c, 3,3,3,4..c, ..., c,c,c,c..c]. Now we only need to force the values to 0 once they exceed s which we achieve with sum [ j | j <= s].

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

APL(Dyalog Classic), 12 bytes

{⍺ ⍺↑∘.⌈⍨⍳⍵}

Try it online!

Any tips on turning this into a train are welocome.

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

APL (Dyalog), 12 bytes

o×⎕≥o←∘.⌈⍨⍳⎕

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Would something like o×⎕≥o←∘.⌈⍨⍳ be allowed, or would you have to assign it to a function in order for that to count? \$\endgroup\$
    – Adalynn
    Commented Jul 21, 2018 at 16:05
  • \$\begingroup\$ @Zacharý my guess is that one would need to be put it inside a tradfn with an argument or a dfns \$\endgroup\$
    – Uriel
    Commented Jul 21, 2018 at 19:17
3
\$\begingroup\$

JavaScript (ES6), 61 bytes

Takes input in currying syntax (c)(s), where s is 1-indexed. Returns a 1-dimensional array.

c=>s=>[...Array(c*c)].map((_,k)=>(k=k%c>k/c?k%c:k/c)<s?-~k:0)

Try it online!

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

Jelly, 6 bytes

⁴Ri»µþ

A full program* taking integers C and S which prints the Jelly representation of a list of lists of integers as defined (1-indexed).

Try it online! (formats the result of the dyad as a grid of numbers for easier reading)

How?

⁴Ri»µþ - Main Link: C, S
     þ - outer product with:
    µ  -   the monadic function (i.e. f(x,y) for x in [1..C] for y in [1..C]):
   »   -     maximum (of x and y)
⁴      -     program's 4th argument = 2nd input = S
 R     -     range = [1,2,3,...S]
  i    -     first index of (the maximum) in (the range) or 0 if not found
       - as a full program: implicit print

* The reason this is a full program is down to the use of the program argument access, . As a dyadic link this code would rely on how the program which is using it is called.
Reusable dyadic link in 8 bytes (taking S on the left and C on the right): RiⱮⱮ»þ`}
Reusable dyadic link in 8 bytes (taking C on the left and S on the right): RiⱮⱮ⁹»þ¤

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

Java 10, 88 bytes

C->S->{var r=new int[C][C];for(;S>0;)for(int s=S--;s-->0;)r[S][s]=r[s][S]=S+1;return r;}

Try it online.

Explanation:

C->S->{                     // Method with two int parameters and int-matrix return-type
  var r=new int[C][C];      //  Result-matrix of size `C` by `C`
  for(;S>0;)                //  Loop as long as `S` is not 0 yet:
    for(int s=S--;s-->0;)   //   Inner loop `s` in the range (`S`, 0]
                            //   (and decrease `S` by 1 in the process with `S--`)
      r[S][s]=r[s][S]=S+1;  //    Set the values at both {`S`,`s`} and {`s`,`S`} to `S+1`
  return r;}                //  Return the result
\$\endgroup\$
2
\$\begingroup\$

PHP, 92 bytes

This is "1-indexing".

<?list(,$c,$s)=$argv;for(;$i++<$c;print"\n")for($j=0;$j++<$c;)echo$s<$i||$s<$j?0:max($i,$j);

To run it:

php -n <filename> <c> <s>

Example:

php -n collapsing_matrice.php 8 6

Or Try it online!

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

Stax, 10 bytes

▓╜.→,cΘ○╤æ

Run and debug it

How it works:

R(Xm]i*xit+J Full program, implicit input.
R            1-based range of S
 (           Right-pad with zeroes to length C
  X          Save to X register
   m         Map (same as here):
    ]          Wrap in list
     i*        repeat by iteration index
       xit     Remove first  elements from X register
          +    Append
           J   Stringify each element, and join by space

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

Excel VBA, 65 bytes

An immediate window function that takes input from [A1:B1] and outputs to the range [C1].Resize([A1],[A1]).

[C1].Resize([A1],[A1])=0:For s=-[B1]To-1:[C1].Resize(-s,-s)=-s:Next

Input / Output

Input is in range [A1:B1]

I/O

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

J, 18 bytes

,~@[{.[:>./~1+i.@]

Much longer than both APL solutions.

Try it online!

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

MATLAB, 58 bytes (Thanks to anonymous user)

function o=f(c,s);o=zeros(c);for j=s:-1:1;o(1:j,1:j)=j;end

Just filling the elements of matrix with appropriate number, running a loop. Maybe possible to be cleverer with arrayfun

\$\endgroup\$
4
  • \$\begingroup\$ You don't need to name the function and you can use zeros(c) which safes some bytes. Also did you see this Octave answer, I guess it would work in Matlab too? \$\endgroup\$ Commented Jul 20, 2018 at 0:48
  • \$\begingroup\$ @OMᗺ Octave you can't name variables inside anonymous functions in matlab. Also, max() have to take same-shape arguments \$\endgroup\$ Commented Jul 20, 2018 at 0:56
  • 1
    \$\begingroup\$ An anonymous user suggested function o=f(c,s);o=zeros(c);for j=s:-1:1;o(1:s,1:s)=j;end. \$\endgroup\$ Commented Jul 20, 2018 at 12:11
  • \$\begingroup\$ @JonathanFrech oh my so much simpler :-( just need to be o(1:j,1:j)=j \$\endgroup\$ Commented Jul 20, 2018 at 19:18
1
\$\begingroup\$

C# (.NET Core), 85 bytes

c=>s=>{var r=new int[c,c];for(;s>0;)for(int j=s--;j-->0;)r[s,j]=r[j,s]=s+1;return r;}

Try it online!

A port of Kevin Cruijssen's answer, which was much better than mine.

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

Python 2, 58 bytes

lambda C,S:[-~max(i%C,i/C)*(i%C<S>i/C)for i in range(C*C)]

Try it online!

Outputs a 1D list of length C*C.

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

Charcoal, 19 bytes

Eθ⪫IEEθ⌈⟦ιλ⟧∧‹λη⊕λ 

Try it online! Link is to verbose version of code. 3 bytes used to convert the output to decimal and format it nicely. Explanation:

 θ                  Input `C`
E                   Map over implicit range
      θ             Input `C`
     E              Map over implicit range
          λ         Inner index
         ι          Outer index
       ⌈⟦  ⟧        Maximium
    E               Map over results
              λ     Current value
               η    Input `S`
             ‹      Less than
                 λ  Current value
                ⊕   Incremented
            ∧       Logical AND
   I                Cast to string
  ⪫                 Join with spaces
                    Implicitly print on separate lines
\$\endgroup\$
1
\$\begingroup\$

Clean, 67 bytes

import StdEnv
$n s=[[if(i>s||j>s)0(max i j)\\i<-[1..n]]\\j<-[1..n]]

Try it online!

Defines $ :: Int Int -> [[Int]] giving an answer using 1-based indexing.

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

Perl 6, 37 bytes

{((^$^c+1 Xmax^$c+1)Xmin$^s+1)X%$s+1}

Try it online!

Returns the matrix as 1-dimensional array.

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

Mathematica 44 bytes

Table[If[i <= s && j <= s, Max[i, j], 0], {i, c}, {j, c}]
\$\endgroup\$
1
  • \$\begingroup\$ Are you certain the whitespace is necessary? I can't test Mathematica but I don't think it is. \$\endgroup\$
    – Wheat Wizard
    Commented Jul 31, 2018 at 13:39
1
\$\begingroup\$

Husk, 11 bytes

T0:´ṪYḣ⁰`R0

Try it online! The function takes S followed by C.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.