36
\$\begingroup\$

Definition

An arrowhead matrix is a matrix that has all entries equal to 0, except the ones on the main diagonal, top row and leftmost column. In other words, the matrix should look like this:

* * * * * *
* * 0 0 0 0
* 0 * 0 0 0
* 0 0 * 0 0
* 0 0 0 * 0
* 0 0 0 0 *

Where each * is any non-zero entry.

Task

Given a square matrix of non-negative integers, check whether it is arrowhead according to the definition above.

You may not take the size of the matrix as input, unless your language’s equivalent to an array is something like a pointer and a length (like C). It will always be at least 3 x 3.

The shortest code in bytes in each language wins.

Input and Output

You can pick among any of the following formats for receiving input:

  • A matrix in the native matrix type (if your language has one)
  • A 2D array1 (an array of 1D arrays, each corresponding to one row)
  • A 1D array (since the matrix is always square)
  • A string (you chose the spacing, but please do not abuse this in any way).

When it comes to providing output, you can either report a truthy / falsy value following the standard decision-problem definition, or choose any two distinct and consistent values.

Moreover, you can take input and give output through any standard method, in any programming language, while taking note that these loopholes are forbidden by default. If want to pick any other format or are unsure about something, please ask in the comments.

1: or your language's equivalent (list, vector, etc.)

Examples

Let's look at the following examples:

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

This is an arrowhead matrix (your programs should report a truthy value), because the elements on the main diagonal are 1 1 1 1, those on the top row are 1 2 2 2 and those on the leftmost column are 1 2 3 4. All other entries are 0, so this satisfies all the conditions.

3 5 6
7 1 0
8 0 0

This matrix is not arrowhead because there is a 0 on the main diagonal.

9 9 9 9
9 9 0 0
9 7 9 0
9 0 0 9

This one is not arrowhead either, because it contains a 7 in place of a 0.

More test cases

Truthy:

[[1, 1, 1], [1, 1, 0], [1, 0, 1]]
[[1, 2, 3, 4], [1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]
[[1, 2, 2, 2], [2, 1, 0, 0], [3, 0, 1, 0], [4, 0, 0, 1]]
[[34, 11, 35, 5], [56, 567, 0, 0], [58, 0, 679, 0], [40, 0, 0, 7]]

Falsy:

[[3, 5, 6], [7, 1, 0], [8, 0, 0]]
[[9, 9, 9, 9], [9, 9, 0, 0], [9, 7, 9, 0], [9, 0, 0, 9]]
[[1, 0, 3, 4], [1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]
[[1, 6, 3, 4], [13, 2, 0, 6], [29, 0, 1, 0], [2, 0, 0, 4]]
\$\endgroup\$
10
  • 1
    \$\begingroup\$ Is it possible that the matrix can contain negative numbers \$\endgroup\$
    – Adalynn
    Dec 20, 2017 at 19:14
  • 2
    \$\begingroup\$ @Zacharý No you may assume they are all non-negative. \$\endgroup\$
    – Mr. Xcoder
    Dec 20, 2017 at 19:15
  • \$\begingroup\$ Pedant: A two dimensional array and a matrix are not the same thing, nor is it the same as an array of arrays. Is input as a two dimensional array acceptable if your language of choice is civilised enough to support multidimensional arrays? \$\endgroup\$
    – Ian Bush
    Dec 20, 2017 at 20:07
  • \$\begingroup\$ @IanBush Yes, a 2D array is totally fine. \$\endgroup\$
    – Mr. Xcoder
    Dec 20, 2017 at 20:09
  • 9
    \$\begingroup\$ @Mr.Xcoder This would be a sufficiently different and interesting challenge if the arrowhead could point in any direction \$\endgroup\$
    – dylnan
    Dec 21, 2017 at 1:27

40 Answers 40

1
2
1
\$\begingroup\$

05AB1E, 23 bytes

ćsøćsε¾èˆ)¼}˜OН)ćs˜ĀP‹

Try it online!

This was initially a comment on Kevin's answer, but they told me I should post it separately, and so I did. I should be able to shave off a couple of bytes shortly.

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

05AB1E, 42 29 28 27 bytes

vyNèP}IнPIøнPPĀiI¦€¦D€às€OQ

Can most definitely without any doubt be golfed quite a bit.. I've only just started with 05AB1E; matrices aren't my strong suit; nor are they 05AB1E's strong suit.. All and all not a great combination..

Try it online or verify all test cases.

Explanation:

vyNè }       # Take the diagonal of the input-matrix
    P        # and calculate its product
             #  i.e. [[1,2,3,4],[5,6,0,0],[7,0,8,0],[9,0,0,10]] → [1,6,8,10] → 480
Iн           # Take the first row
  P          # and take its product as well
             #  i.e. [[1,2,3,4],[5,6,0,0],[7,0,8,0],[9,0,0,10]] → [1,2,3,4] → 24
Iøн          # Take the first column
   P         # and take its product as well
             #  i.e. [[1,2,3,4],[5,6,0,0],[7,0,8,0],[9,0,0,10]] → [1,5,7,9] → 315
      P      # Take the product of all three results,
             #  i.e. [480, 24, 315] → 3628800
       Āi    # If this result is larger than 0
             # (which means the diagonal, first row, and first column contain no zeros)
             #   i.e. 3628800 > 0 → 1
I¦           #  Take the input and remove the first row
             #   i.e. [[1,2,3,4],[5,6,0,0],[7,0,8,0],[9,0,0,10]]
             #          → [[5,6,0,0],[7,0,8,0],[9,0,0,10]]
  €¦         #  And also remove the first item of each row
             #   i.e. [[5,6,0,0],[7,0,8,0],[9,0,0,10]] → [[6,0,0],[0,8,0],[0,0,10]]
    D        #  Duplicate that
     ۈ      #  Take the max of each
             #   i.e. [[6,0,0],[0,8,0],[0,0,10]] → [6,8,10]
     s€O     #  As well as the sum of each
             #   i.e. [[6,0,0],[0,8,0],[0,0,10]] → [6,8,10]
        Q    #  And check if all maximums and sums are equal
             #  (this means each column and row only contains two numbers (row/column
             #  and diagonal); everything else is a zero)
\$\endgroup\$
6
  • 1
    \$\begingroup\$ ćsøćsε¾èˆ)¼}˜OН)ćs˜ĀP‹ should save you 6 bytes. Try it online! This is most certainly golfable though, but I don't have time to shorten it further now. But nice approach! \$\endgroup\$
    – Mr. Xcoder
    May 26, 2018 at 11:37
  • \$\begingroup\$ @Mr.Xcoder Nice approach as well! Hmm, but it's so different from my approach that I have the feeling it would be better as a separated answer.. If you want you can post it yourself, or I will add it to my answer, but also leave the 29 byte answer (or 28, since Ā instead of 0› would save a byte - was unable to find the one-char command for >0..) \$\endgroup\$ May 26, 2018 at 11:55
  • \$\begingroup\$ I'll leave that up to you; if you don't include the 23-byter into your answer, I'll post it as a separate one when I get the chance to golf it further. I'm fine either way :) \$\endgroup\$
    – Mr. Xcoder
    May 26, 2018 at 11:58
  • \$\begingroup\$ @Mr.Xcoder Both are also fine by me, so you can post it yourself, since you are the one that came up with it. Let me know when you have, then I will add a link to your shorter 05AB1E answer in mine (and upvote it of course). Normally I wouldn't mind replacing my code with yours, but since the approaches are so different this time you can post it as a separated answer. :) (Have a nice weekend!) \$\endgroup\$ May 26, 2018 at 12:04
  • \$\begingroup\$ Sure, that works. Have a nice weekend too! \$\endgroup\$
    – Mr. Xcoder
    May 26, 2018 at 12:05
1
\$\begingroup\$

R, 52 bytes

function(m)all(rbind(1,cbind(1,diag(nrow(m)-1)))-!m)

Try it online!

One-liner doing the same as the other R solution by Giuseppe for the same byte count. Giuseppe managed to save one byte before I even posted this!

\$\endgroup\$
0
\$\begingroup\$

JavaScript ES6, 97 85 82 bytes

x=>x.map((_,i)=>p(0,i)+p(i,0)+p(i,i),p=(i,j)=>x[i][j]=+!x[i][j])&&!/[1-9]/.test(x)

F = 
x=>x.map((_,i)=>p(0,i)+p(i,0)+p(i,i),p=(i,j)=>x[i][j]=+!x[i][j])&&!/[1-9]/.test(x)

console.log(F([[1,2,3,4,5,6],[9,5,0,0,0,0],[4,0,3,0,0,0],[4,0,0,3,0,0],[4,0,0,0,3,0],[4,0,0,0,0,3]]))

console.log(F([[1,2,3,4,5,6],[9,5,0,0,0,0],[4,0,3,0,0,0],[4,0,7,3,0,0],[4,0,0,0,3,0],[4,0,0,0,0,3]]))

console.log(F([[1,2,0,4,5,6],[9,5,0,0,0,0],[4,0,3,0,0,0],[4,0,0,3,0,0],[4,0,0,0,3,0],[4,0,0,0,0,3]]))

\$\endgroup\$
1
  • \$\begingroup\$ @Mr. Xcoder For C the equ of an array is a pointer and a length. For JavaScript I modified so it doesn't take the length \$\endgroup\$
    – l4m2
    Dec 20, 2017 at 20:02
0
\$\begingroup\$

C# (.NET Core), 73 + 18 = 91 bytes

using System.Linq;
a=>a.SelectMany((b,i)=>b.Select((n,j)=>i<1||j<1||i==j?n>0:n<1)).All(v=>v)

Try it online!

The input is an array of integer-arrays (int[][]) the output is bool.

Ungolfed full program:

using System.Linq;
class Program
{
    static void Main(string[] args)
    {
        System.Func<int[][], bool> f =
        a => a.SelectMany(                          //Iterates through all elements of the outer array, applies the following function and flattens the result
            (b, i) =>                               //b is the inner array, i is it's index
                b.Select(                           //Iterates through all elements of the current array (b) and applies the following function
                    (n, j) =>                       //n is the integer, j is it's index in the inner array (b)
                        i < 1 || j < 1 || i == j ?  //If the current element is in the first row, first column or the diagonal
                        n > 0 :                     //Check if the element is not 0
                        n < 1))                     //else check if the element is 0
            .All(v => v);                           //Check if the value of all elements of the flattened result is true, the result is returned implicitly

        //Truthy test cases
        System.Console.WriteLine(f(new int[][] { new[] { 1, 1, 1 }, new[] { 1, 1, 0 }, new[] { 1, 0, 1 } }));
        System.Console.WriteLine(f(new int[][] { new[] { 1, 2, 3, 4 }, new[] { 1, 1, 0, 0 }, new[] { 1, 0, 1, 0 }, new[] { 1, 0, 0, 1 } }));
        System.Console.WriteLine(f(new int[][] { new[] { 1, 2, 2, 2 }, new[] { 2, 1, 0, 0 }, new[] { 3, 0, 1, 0 }, new[] { 4, 0, 0, 1 } }));
        System.Console.WriteLine(f(new int[][] { new[] { 34, 11, 35, 5 }, new[] { 56, 567, 0, 0 }, new[] { 58, 0, 679, 0 }, new[] { 40, 0, 0, 7 } }));

        //Falsey test cases
        System.Console.WriteLine(f(new int[][] { new[] { 3, 5, 6 }, new[] { 7, 1, 0 }, new[] { 8, 0, 0 } }));
        System.Console.WriteLine(f(new int[][] { new[] { 9, 9, 9, 9 }, new[] { 9, 9, 0, 0 }, new[] { 9, 7, 9, 0 }, new[] { 9, 0, 0, 9 } }));
        System.Console.WriteLine(f(new int[][] { new[] { 1, 0, 3, 4 }, new[] { 1, 1, 0, 0 }, new[] { 1, 0, 1, 0 }, new[] { 1, 0, 0, 1 } }));
        System.Console.WriteLine(f(new int[][] { new[] { 1, 6, 3, 4 }, new[] { 13, 2, 0, 6 }, new[] { 29, 0, 1, 0 }, new[] { 2, 0, 0, 4 } }));
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Suggest i<1|j<1|i==j instead of i<1||j<1||i==j \$\endgroup\$
    – ceilingcat
    Aug 31, 2020 at 22:49
0
\$\begingroup\$

PHP, 80 bytes

foreach($_GET[a]as$i=>$r)foreach($r as$k=>$v)($i&&$k&&$i!=$k)==$v&&$b=1;echo!$b;
\$\endgroup\$
0
\$\begingroup\$

APL NARS, 48 bytes, 24 chars

{(×⍵)≡1⍪1,(⍳∘.=⍳)¯1+↑⍴⍵}

test:

   y←{(×⍵)≡1⍪1,(⍳∘.=⍳)¯1+↑⍴⍵}
   a1 a2 a3
 1 2 2 2   3 5 6   9 9 9 9 
 2 1 0 0   7 1 0   9 9 0 0 
 3 0 1 0   8 0 0   9 7 9 0 
 4 0 0 1           9 0 0 9 
   y¨a1 a2 a3
1 0 0 
\$\endgroup\$
0
\$\begingroup\$

Java 8, 95 bytes

A lambda from int[][] to int (0 is false, 1 is true).

m->{int i=0,d=m.length,r,c,f=1;while(i<d*d)f=m[r=i/d][c=i++%d]<1==(r*c*(r-c)!=0)?f:0;return f;}

Try It Online

Ungolfed

m -> {
    int
        i = 0,
        d = m.length,
        r, c,
        f = 1
    ;
    while (i < d * d)
        f =
            m[r = i/d][c = i++%d] < 1
                == (r * c * (r-c) != 0) ?
                    f
                    : 0
        ;
    return f;
}
\$\endgroup\$
0
\$\begingroup\$

Ruby, 65 bytes

->a{(r=0...a.size).all?{|i|r.all?{|j|(a[i][j]>0)^(i!=j&&i*j>0)}}}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Attache, 33 bytes

{DrawMatrix["\\<:^>",#_]==Clip!_}

Try it online!

DrawMatrix takes a string and a matrix size and "draws" it. In this case, we generate a base arrowhead matrix, like so:

\<:^>
\      draw a line from the top left to the bottom right
 <:    move to the left
   ^   draw a line up to the top
    >  draw a line right to the left

Then, we check for equality with the input matrix, replacing nonzeroes with 1.

\$\endgroup\$
1
2

Your Answer

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

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