Is my Matrix Arrowhead?

33

2

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

Mr. Xcoder

Posted 2017-12-20T19:00:16.643

Reputation: 39 774

1Is it possible that the matrix can contain negative numbers – Zacharý – 2017-12-20T19:14:26.893

2@Zacharý No you may assume they are all non-negative. – Mr. Xcoder – 2017-12-20T19:15:24.477

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? – Ian Bush – 2017-12-20T20:07:27.320

@IanBush Yes, a 2D array is totally fine. – Mr. Xcoder – 2017-12-20T20:09:54.147

Can we take the input as a 1D matrix, and an int determining the size, or must we determine the size ourselves? – FlipTack – 2017-12-20T20:16:21.823

@FlipTack You must determine the size yourself. Clarified in the post. – Mr. Xcoder – 2017-12-20T20:17:05.500

9@Mr.Xcoder This would be a sufficiently different and interesting challenge if the arrowhead could point in any direction – dylnan – 2017-12-21T01:27:51.380

@dylnan Feel free to post it m, if you want. – Mr. Xcoder – 2017-12-21T06:06:24.263

In languages such as C, may we have the length? Otherwise the easiest (only?) way to check the length of the 2D array is to spawn a thread, get it to try to seek past the end of the end and hope it segfaults at the right place. – wizzwizz4 – 2017-12-22T20:00:28.610

@wizzwizz4 Yes, for langs like C. or your language's equivalent implies that, since the equivalent in C is a pointer and a length, although You may not take the size of the matrix as input kind of contradicts that. Will clarify. – Mr. Xcoder – 2017-12-22T20:02:37.087

Answers

15

Javascript (ES6), 48 47 bytes

Saved 1 byte thanks to edc65

m=>m.some((r,y)=>r.some((c,x)=>(x*y&&x!=y)^!c))

Returns false for arrowhead matrices and true for non-arrowhead matrices (allowed since any two distinct values can be used to represent true and false)

Test cases:

f=m=>m.some((r,y)=>r.some((c,x)=>(x*y&&x!=y)^!c))
console.log(f([[1, 1, 1], [1, 1, 0], [1, 0, 1]]))
console.log(f([[1, 2, 3, 4], [1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]))
console.log(f([[1, 2, 2, 2], [2, 1, 0, 0], [3, 0, 1, 0], [4, 0, 0, 1]]))
console.log(f([[34, 11, 35, 5], [56, 567, 0, 0], [58, 0, 679, 0], [40, 0, 0, 7]]))
console.log(f([[3, 5, 6], [7, 1, 0], [8, 0, 0]]))
console.log(f([[9, 9, 9, 9], [9, 9, 0, 0], [9, 7, 9, 0], [9, 0, 0, 9]]))
console.log(f([[1, 0, 3, 4], [1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]))
console.log(f([[1, 6, 3, 4], [13, 2, 0, 6], [29, 0, 1, 0], [2, 0, 0, 4]]))

Herman L

Posted 2017-12-20T19:00:16.643

Reputation: 3 611

Now that's a truly clever approach! – Mr. Xcoder – 2017-12-20T20:08:36.897

1could this work ? f=m=>m.some((r,y)=>r.some((c,x)=>(x*y&&x!=y)^!c)) – edc65 – 2017-12-20T22:21:36.320

@edc65 Without the f= of course ;-) – Neil – 2017-12-21T01:04:14.593

11

J, 21 20 19 17 15 bytes

-4 bytes thanks to @GalenIvanov.

*-:1,1,.=&/:@}.

Takes input as a matrix (rank 2 array).

Try it online!

Explanation

Let the edit history be a lesson to you not to golf and write an explanation at the same time.

* -: 1, 1,. = & /: @ }.  Let m be the input matrix.
            = & /: @ }.  Identity matrix 1 smaller than m.
                     }.    Behead (m without its first row).
                   @       Composed with.
                /:         Grade up (get len(m) - 1 unique elements)
              &            Composed with.
            =              Self-classify (compare equality with
                           unique elements)
        1,.              Prepend a column of 1s
     1,                  Prepend a row of 1s
*                        Signum (0 becomes 0, n > 0 becomes 1)
  -:                     Does it match the generated arrowhead matrix?

Visual explanation

Note that this is done on the REPL (inputs are given starting with three spaces and output are given without any leading spaces). Because of that, I sometimes omit composition functions like @ and & since things on the REPL are evaluated right-to-left (functions are more complex).

Suppose you have the following sample matrix:

   ] m =. 4 4 $ 1 2 3 4 1 1 0 0 1 0 1 0 1 0 0 1
1 2 3 4
1 1 0 0
1 0 1 0
1 0 0 1

First, I'd like to explain (and give a shoutout to) @GalenIvanov's very clever way of generating the identity matrix, which is the following =&/:@}..

First, we behead the input matrix (}.).

   }. m
1 1 0 0
1 0 1 0
1 0 0 1

Then we get the indices each row would be in were the rows sorted using /:-grade up.

   /: }. m
2 1 0

Note that the resulting indices are unique: the list has no duplicate elements (and why would it? There's no way to place two elements in the same position in an array).

Finally, we use the niche but helpful =-self-classify. This monad compares each unique element to all of the other elements in an array. Remember how I mentioned it was important that the resulting indicies are unique? Since =-self-classify does the comparisons in the order that the unique elements appear in the list, the resulting output will be the identity matrix for a unique input (this is why =@i. is how you can make an identity matrix of a given length).

   = /: }. m
1 0 0
0 1 0
0 0 1
   NB. This is what is happening
   (2 = 2 1 0) , (1 = 2 1 0) ,: (0 = 2 1 0)
1 0 0
0 1 0
0 0 1

Once we have the identity matrix, it's a matter of adding a row of ones and a column of ones, which is done very simply (if given an atom - i.e. a single element - the , family will repeat it to fill when it's being added):

   1,. (=&/:@}. m)
1 1 0 0
1 0 1 0
1 0 0 1
   1, (1,. =&/:@}. m)
1 1 1 1
1 1 0 0
1 0 1 0
1 0 0 1

Then we simply compare the generated arrowhead matrix to the signum of the input matrix.

   * m
1 1 1 1
1 1 0 0
1 0 1 0
1 0 0 1
   (* m) -: (1, 1,. =&/:@}. m)
1

cole

Posted 2017-12-20T19:00:16.643

Reputation: 3 526

2

Isn't * enough instead of 0@< (for 17 bytes)? Try it

– Galen Ivanov – 2017-12-20T19:57:44.847

1@GalenIvanov good catch, I think so. Thanks! Time to re-edit the explanation lol. – cole – 2017-12-20T19:58:33.263

1

I think I found a new way to generate the identity matrix: =&/: When I combined it with }., I got this *-:1,1,.=&/:@}. for 15 bytes Try it online!

– Galen Ivanov – 2017-12-20T22:04:04.250

1@GalenIvanov brilliant approach (both the use of /:-grade and }.-behead), thank you again! I'll edit it in. – cole – 2017-12-20T22:10:05.103

Hmm, in fact *-:1,1,.=@}. works just fine - no need of fancy way to find the identity matrix. You can generate an identity matrix from the square matrix itself simply by =. So drop one row with }., make the identity matrix with =, add a row and a column with 1 and so on. – Galen Ivanov – 2017-12-20T22:17:35.013

@GalenIvanov does this work if there are duplicate rows in the matrix? – cole – 2017-12-20T22:20:09.200

You are right, it doesn't work for duplicate rows – Galen Ivanov – 2017-12-20T22:22:14.667

9

Wolfram Language (Mathematica), 47 bytes

Clip@#==Array[If[1<#!=#2>1,0,1]&,{1,1}Tr[1^#]]&

Try it online!

Explanation: Clip@# replaces all the non-zero numbers in the matrix with 1s, then we compare this to an array with dimensions {1,1}Tr[1^#] = {Length@#, Length@#} with 0 in position i,j when 1 < i != j > 1, and 1 otherwise.

(Roughly based on Uriel's answer.)

Here's another idea that's 16 bytes longer — feel free to steal it if you can golf it down:

Union@@Array[{1,#}~Tuples~2&,Length@#]==Most@Keys@ArrayRules@#&

Try it online!

Not a tree

Posted 2017-12-20T19:00:16.643

Reputation: 3 106

8

APL (Dyalog Classic), 19 16 15 13 bytes

-1 byte thanks to @ErikTheOutgolfer

(⎕IO←0)

×≡(∧=⌊)/¨∘⍳∘⍴

Try it online!

-2 bytes thanks to @ngn and @H.PWiz

How?

(2D input matrix S)

  • ×≡ Check whether S is positive only on ...
  • (∧=⌊ ... the diagonals or the top row and left column ...
  • )/¨∘⍳∘⍴ ... of S.

Zacharý

Posted 2017-12-20T19:00:16.643

Reputation: 5 710

nice utilization of ⍳∘⍴ for cartesian product. – Uriel – 2017-12-20T21:47:33.633

×≡(=/∨1∊⊢)¨∘⍳∘⍴ – Erik the Outgolfer – 2017-12-21T12:33:17.200

1(=/∨1∊⊢) -> (~≠⌊⌊)/ – ngn – 2018-01-13T12:58:12.807

2@ngn Even better: (∧=⌊)/, of course both require ⎕IO←0 – H.PWiz – 2018-02-13T03:23:43.830

7

APL (Dyalog), 21 18 17 bytes

×≡1⍪1,(=/¨∘⍳1-⍨⍴)

Try it online!

How?

This one goes the other way -

=/¨∘⍳ - creates the identity matrix

1-⍨⍴ - for n - 1

1⍪1, - prepends a column and a row of 1s

- compares with

× - the original matrix, after it has gone an element-wise signum-ing

Uriel

Posted 2017-12-20T19:00:16.643

Reputation: 11 708

7

PowerShell, 112 108 bytes

param($a)$o=+!(0-in$a[0]);1..($x=$a.count-1)|%{$i=$_;0..$x|%{$o*=(!($y=$a[$i][$_]),$y)[!$_-or$_-eq$i]}};!!$o

Try it online!

Takes input and manipulates as an array-of-arrays, since PowerShell doesn't support matrices (outside of the .NET Direct3D transform matrices support, which is something entirely different).

The whole algorithm is based on the fact that non-zero numbers are truthy and zero is falsey in PowerShell, and using multiplication to determine those truthy/falsey values.

We first take the first row, $a[0], and check whether 0 is -in that array, store that into our $output variable. If anything in that row is zero, then the $o is also zero, otherwise it's one, done by a quick cast-to-int with +.

Next we loop from 1 up to $a.count-1, setting $x along the way -- we're going to be looping through each row one at a time.

Each iteration we set helper variable $i to keep track of what row we're on, then loop from 0 to $x to iterate each element in this row. Inside the inner loop, we're again multiplying $o, this time by selecting from a tuple setup as a pseudo-ternary operator.

The tuple's conditional, !$_-or$_-eq$i, says "when we're on the 0th column, or the column matches the row (i.e., the main diagonal)" to select the second half of the tuple when truthy or the first half when falsey. The tuple is composed of !($y=$a[$i][$_]), $y. The first half sets $y for golfing the second half, but either way we're selecting the current element. The first half does Boolean negation on it, while the second half just takes the element as-is. Thus, if we're not on the 0th column nor the main diagonal, we ensure that the element is zero by taking the Boolean-not of it. Similarly, we ensure the 0th column or main diagonal is non-zero by simply taking it.

So now that we've iterated through every element in the matrix, $o is either going to be 0 if some element was incorrect, or some non-zero integer if it is an arrowhead matrix. We double-Boolean-not that to get either False or True respectively, to make our output consistent, and that's left on the pipeline where printing is implicit.

AdmBorkBork

Posted 2017-12-20T19:00:16.643

Reputation: 41 581

+ = [int]? That is nice. – root – 2017-12-21T20:34:33.383

@root One of the PowerShell tips.

– AdmBorkBork – 2017-12-21T21:26:33.290

7

Jelly, 14 12 bytes

ŒDµḢ;Ḣ€Ȧ>FẸ$

-2 bytes from Pietu1998

Try it online!

Explanation

[[9,7,1],
 [7,1,0],
 [7,0,1]]

Use the above matrix as an example input.

ŒDµḢ;Ḣ€Ȧ>FẸ$
ŒD              Diagonals → [[9, 1, 1], [7, 0], [1], [7], [7, 0]]
  µ             New monadic link
   Ḣ            Head → [9, 1, 1]. Alters diagonals list.
    ;Ḣ€         Append with the head of each of the other diagonals → [9, 1, 1, 7, 1, 7, 7]
       Ȧ        Logical all → 1
         FẸ$    Flatten what's left in diagonals then take logical any → [[0],[],[],[0]] → [0,0] → 0
        >       Matrix is an arrowhead iff result of Ȧ > result of Ẹ

dylnan

Posted 2017-12-20T19:00:16.643

Reputation: 4 993

@wizzwizz4 I'm not sure what you mean – dylnan – 2017-12-22T21:09:12.857

@wizzwizz4 this code shows how the elements of the matrix are regrouped. It does take the top, left and main diagonal. Is this what you meant?

– dylnan – 2017-12-23T16:34:31.100

I meant the actual visual representation of the code that you provided in your explanation. I was trying to be funny but it obviously didn't work. I'll clean up these comments. – wizzwizz4 – 2017-12-23T16:38:49.403

6

MATL, 15 bytes

gtZyXy,!llY(]X=

Input is a matrix (using ; as row separator). Output is 1 for arrowhead, 0 otherwise.

Try it online! Or verify all test cases.

Explanation

g        % Implicit input. Convert to logical values (nonzero becomes true and
         % zero becomes false)
t        % Duplicate
Zy       % Size
Xy       % Identity matrix of that size
,        % Do twice
  !      %   Transpose
  ll     %   Push 1 twice
  Y(     %   Write 1 at all entries of row 1
]        % End
X=       % Are the two matrices (input and constructed) equal? Implicit display

Luis Mendo

Posted 2017-12-20T19:00:16.643

Reputation: 87 464

1What exactly is Indeity matrix? – Erik the Outgolfer – 2017-12-20T20:10:25.900

13@EriktheOutgolfer obviously a matrix containing a deity. – cole – 2017-12-20T20:11:52.593

5@cole perhaps related to a matrix over the Elysian field – jld – 2017-12-21T01:51:45.963

5

R, 78 70 69 68 54 53 bytes

function(m){d=diag(nrow(m))
d[1,]=d[,1]=1
all(d!=!m)}

Try it online!

Porting Luis Mendo's answer is much shorter than my former approach.

Thanks to rturnbull for pointing out a bug, and golfing down a byte!

old answer, 68 bytes:

function(m,i=which(!m,T))all(i[,1]-i[,2],i!=1,sum(m>0)==3*nrow(m)-2)

Try it online!

duckmayr's answer tests that all entries on the main diagonal and first row/column (m[i]) are nonzero and the rest (m[-i]) are zero, using some nice arithmetic to get the diagonal and the first row.

This answer, however, tests to make sure that (1) zero entries are not on the main diagonal or the first row/column, and (2) that there are, given an n x n matrix, 3*n-2 nonzero entries.

which returns the indices where its input is TRUE, and with the optional arr.ind=T, returns an array of indices for each array dimension, in this case, two.

Hence when any(i[,1]==i[,2]), there exists a zero on the diagonal, and when any(i==1), there exists a zero in the first row or the first column.

Finally, a little arithmetic shows that the number of nonzero entries must be 3*n-2, n from the first column, n-1 from the diagonal, and n-1 from the first row.

Giuseppe

Posted 2017-12-20T19:00:16.643

Reputation: 21 077

This doesn't seem to work for arrow matrices where the values are not 1. Did you mean all(!m==!d) in the last line? – rturnbull – 2017-12-22T07:17:13.573

@rturnbull ah! Thank you. R operator syntax is so weird. I really meant (!!m)==d but ! has lower precedence than ==. I think d==!!m should do the trick, though. – Giuseppe – 2017-12-22T11:53:20.700

It looks like d!=!m does the same, for one byte less. You can save another byte by using pryr::f syntax rather than function too. – rturnbull – 2017-12-22T20:27:02.097

I tried to golf it but the best i can do is still 53.

– JayCe – 2018-06-05T13:39:44.943

@JayCe nah both your answer and mine can be golfed to 52, and I'm not sure why it didn't occur to me before... I'd post yours as separate; the one-line approach is quite nice and I suspect there may be some more room for improvement in yours – Giuseppe – 2018-06-05T14:07:18.273

hum... all(d-!m)? – JayCe – 2018-06-05T14:13:09.577

@JayCe got it in one shot! – Giuseppe – 2018-06-05T14:13:28.593

!sd(d+!m) brings it down to 52 as well. – JayCe – 2018-06-05T18:35:38.433

5

Python 2, 75 bytes

lambda m,E=enumerate:all((x[j]>0)-(i>0<j!=i)for i,x in E(m)for j,y in E(m))

Try it online!

Python 2, 85 bytes

Taking the array as a 1D matrix:

def f(m):s=len(m)**.5;print all((v<1)^(0in(p>s,p%s,p//s-p%s))for p,v in enumerate(m))

Try it online!

FlipTack

Posted 2017-12-20T19:00:16.643

Reputation: 13 242

Did you mean "1D matrix" in the top solution? – NikoNyrh – 2017-12-22T12:11:47.813

@NikoNyrh oops, fixed – FlipTack – 2017-12-22T13:35:35.183

5

C (gcc), 80 75 bytes

i;f(A,n)int*A;{for(i=0;i<n*n;i++)n=A[i]>0^(i<n||i%n<1||i/n==i%n)?0:n;n=!n;}

Try it online!

Saved 5 bytes thanks to scottinet!

Reused the test code from this answer.

Linearly scans the array for any incorrect values, returning 0 for an arrowhead matrix and 1 otherwise. We check by computing the exclusive or of whether the item at a given position is zero and whether that position is on the arrow.

Encoding the information of the 2D array into one dimension leads to a fairly simple set of conditions. If we let i be our 0-based index into the n dimensional array, then i<n describes the first row. Similarly, i%n==0 describes the first column and i/n==i%n describes the diagonal.

The best trick I found for handling the return is to set the dimension to zero when encountering an error. This causes the loop to terminate immediately, then returning the logical negation of the dimension will give us one of two distinct values. scottinet found the way to make GCC return it more nicely.

FryAmTheEggman

Posted 2017-12-20T19:00:16.643

Reputation: 16 206

-2 bytes with some more golfing – scottinet – 2017-12-21T09:28:09.137

and an additional -4 bytes by abusing gcc's way of returning values

– scottinet – 2017-12-21T09:29:34.447

@scottinet Thanks! I was having trouble figuring out which value I should set to use that trick. – FryAmTheEggman – 2017-12-21T15:23:32.540

Actually, I don't believe your first golf works. It passed the test cases because there was never a zero in the first position. Added a case and reverted that change. – FryAmTheEggman – 2017-12-21T15:32:53.690

int test0[] = {0, 1, 1, 1, 1, 0, 1, 0, 1}; printf("%d\n", f(test0, 3)); Has to return 0 not 1 (if is the 3x3 matrx 011 110 101) because a[0,0] is 0 – RosLuP – 2018-01-13T10:08:44.627

@RosLuP The function returns 0 for an arrowhead matrix, and 1 for a non-arrowhead matrix. I do not believe there is a problem, in this case. – FryAmTheEggman – 2018-01-13T16:51:22.093

3

Python 2, 92 90 bytes

def f(m):l=len(m);return all((m[a%l][a/l]<1)^any([a<l,a%l<1,a/l==a%l])for a in range(l*l))

Try it online!

Credits

Neil

Posted 2017-12-20T19:00:16.643

Reputation: 2 417

3

Haskell, 62 bytes

-3 bytes thanks to Mr. Xcoder. -13 bytes thanks to user28667. -5 bytes thanks to Zgarb.

z=zip[0..]
f m=and[(i==j||i*j<1)==(a>0)|(i,r)<-z m,(j,a)<-z r]

Try it online!

totallyhuman

Posted 2017-12-20T19:00:16.643

Reputation: 15 378

180 bytes.... You almost always forget about <1 and such tricks? :P – Mr. Xcoder – 2017-12-20T20:31:58.790

1(x==y||x==0||y==0)==(m!!y!!x/=0) should be shorter – user28667 – 2017-12-20T21:44:36.930

162 bytes by zipping instead of indexing, and doing x*y<1. – Zgarb – 2017-12-21T00:01:56.370

3

Jelly, 10 bytes

ŒDµḢṠQṭT€E

Try it online!

Dennis

Posted 2017-12-20T19:00:16.643

Reputation: 196 637

3

Python 3, 72 71 bytes

lambda x,e=enumerate:any(0**n^(0<i!=j>0)for i,r in e(x)for j,n in e(r))

Thanks to @xnor for golfing off 1 byte!

Try it online!

Dennis

Posted 2017-12-20T19:00:16.643

Reputation: 196 637

I think 0<i!=j>0 saves a byte, – xnor – 2017-12-23T00:30:01.400

@xnor Thanks! I don't think I've ever re-used a number in a comparison chain... – Dennis – 2017-12-23T00:37:12.923

2

APL+WIN, 36 33 bytes

(↑⍴m)=+/(+⌿m)=+/m←×m×n∘.×n←⍳↑⍴m←⎕

Prompts for screen input of an APL 2d matrix.

Graham

Posted 2017-12-20T19:00:16.643

Reputation: 3 184

2

Clojure, 212 206 188 bytes

-6 bytes by removing some missed spaces, and shortcutting range. I might have to let this sit so I can think of a better way.

-18 bytes thanks to @NikoNyrh, and creating shortcuts for map.

(fn[m](let[r range a map z zero?](and(every? #(not(z %))(concat(m 0)(rest(a #(% 0)m))(a get m(r))))(every? z(apply concat(into(a #(take(dec %)%2)(r)(a rest m))(a take-last(r)(reverse(rest m)))))))))

Awful, just awful. I don't know why I can't wrap my head around a reasonable solution.

Takes a nested vector as input.

(defn arrowhead? [matrix]
  (let [; Get the 0th cell of the 0th row, then the 1st cell of the 1st row...
        main-diagonal (map get matrix (range))

        ; Get the 0th cell of each row
        first-col (rest (map #(% 0) matrix))
        arrowhead (concat (matrix 0) first-col main-diagonal)

        ;
        right-rest (map take-last (range) (reverse (rest matrix)))
        left-rest (map #(take (dec %) %2) (range) (map rest matrix))
        rest-matrix (apply concat (into left-rest right-rest))]

    ; And check them
    (and (every? pos? %) arrowhead
         (every? zero? rest-matrix))))

I tried rewriting this from scratch using a different method, and it ended up longer. Instead of manually carving out the "rest" sections of the matrix, I instead decided to try generating all the coordinates in the matrix, generating the coordinates of the arrowhead, then use clojure.set/difference to get the non-arrowhead cells. Unfortunately, the call to that built-in is costly:

223 bytes

(fn[m](let[l(range(count m))g #(get-in m(reverse %))e every? a(for[y l x l][x y])k #(map % l)r(concat(k #(do[% %]))(k #(do[0%]))(k #(do[% 0])))](and(e #(zero?(g %))(clojure.set/difference(set a)(set r)))(e #(pos?(g %)))r)))

(defn arrowhead? [matrix]
  (let [length-range (range (count matrix))
        get-cell #(get-in matrix (reverse %))
        all-coords (for [y length-range
                         x length-range]
                     [x y])

        k #(map % length-range)

        diag (k #(do[% %]))
        top-side (k #(do [0 %]))
        left-side (k #(do [% 0]))
        arrowhead (concat diag top-side left-side)

                   ; 22 bytes! Ouch
        rest-cells (clojure.set/difference (set all-coords) (set arrowhead))]

    (and (every? #(zero? (get-cell %)) rest-cells)
         (every? #(pos? (get-cell %)) arrowhead))))

Carcigenicate

Posted 2017-12-20T19:00:16.643

Reputation: 3 295

There is quite lot of room for improvement, for example #(drop 1 %) is same as rest and #(not(zero? %)) is same as pos? (as we have non-negative numbers). You might want to have a look at my 128-byte answer, which has similar approacha s this one. After implementing that I realized that it is a lot shorted to deal with index-based access in a for-loop. – NikoNyrh – 2017-12-22T15:28:36.113

@NikoNyrh Ya, I wasn't in a very good groove that day. I don't know how I forgot about rest. I should probably just scrap this attempt and try again. – Carcigenicate – 2017-12-22T15:35:04.493

2

Pyth, 22 21 bytes

This is definitely not the language for matrix manipulation.

.As.e+!MWk.Db,0k,@bkh

For each row b and its index k in the matrix (.e), grabs the first and kth entries (left side and diagonal) with ,@bkh and (+) all the other entries with .Db,0k. If k isn't 0 to correspond to the first row (Wk), then !not M all of those entries. Once all of those have been selected, make sure all of them are true. (.As) If there's a 0 where there shouldn't be, then the corresponding location will be grabbed as is and mess up the and, and if there's a nonzero where there shouldn't be, it'll be ! notted to 0, which is also false.

Test suite.

-1 bytes for swapping the orders around.

Steven H.

Posted 2017-12-20T19:00:16.643

Reputation: 2 841

1Wow this solution is really nice given that Pyth is quite parallel with matrix manipulation. Probably up for another Pyth duel tomorrow :P – Mr. Xcoder – 2017-12-20T21:24:36.430

You might be able to shorten this using either @VQUQ or .DVQUQ For diagonals / deleting diagonals. But that would require a completely different approach. Not sure though... (BTW forgot to update link?) – Mr. Xcoder – 2017-12-20T21:42:56.000

@Mr.Xcoder Fixed link, I'll try to mess around with other strategies tomorrow. – Steven H. – 2017-12-20T22:24:28.687

I arrived at an alternative 21-byter using my VQUQ idea: >.A++hCQhQ.(VQUQsstCt. This seems highly redundant, though. You might be able to tweak it in order to save a few bytes. – Mr. Xcoder – 2017-12-21T14:37:53.170

2

Pip, 31 23 22 bytes

{0<_!=B>0MC#a==0=_MMa}

This is a function that takes a 2D nested list of numbers. Try it online!

Explanation

A whole lot of comparisons going on here. The first thing to know is that comparison operators in Pip can be chained together, like in Python: 5>4>3 is 5>4 and 4>3 (true), not (5>4)>3 (false). The second is that this doesn't apply to ==, the "exactly equals" operator. Another difference: regular comparisons have higher precedence than the mapping operators MC and MM and can be used in lambda expressions, while == has lower precedence and can't.

{                    }  Define a function with argument a:
 0<_!=B>0MC#a            Generate a matrix (as nested lists) that has 0 on the first row,
                          first column, and main diagonal, and 1 elsewhere (see below for
                          details)
               0=_MMa    Map the function 0=_ to the elements of the elements of a,
                          generating a matrix that is 0 where a is nonzero and vice versa
             ==          Test if the two matrices are equal, returning 0 or 1 accordingly

To generate the first matrix, we use MC, "map-coords." This operator takes a number, generates a square coordinate grid of that size, and maps a function to each (x,y) coordinate pair, returning a list of lists of the results. For example, {a+b} MC 3 would give the result [[0; 1; 2]; [1; 2; 3]; [2; 3; 4]].

Here, the size of the grid is #a, the size of our original argument. The function is 0<_!=B>0, which is a shorter way of writing {0 < a != b > 0}:

{        }  Function; a and b are the arguments (in our case, row and column)
 0<a        Return 1 (truthy) if a is greater than 0
    !=b     and a is not equal to b
       >0   and b is greater than 0

This returns 0 for the first row/column and the main diagonal, and 1 elsewhere.

DLosc

Posted 2017-12-20T19:00:16.643

Reputation: 21 213

2

Husk, 12 11 bytes

S≡ȯ´Ṫ§^*=ŀL

Try it online!

Explanation

S≡ȯ´Ṫ§^*=ŀL  Input is a k×k array A.
          L  The length, k.
         ŀ   The range [0,1..k-1].
  ȯ´Ṫ        Outer product with itself by this function:
              Arguments are two numbers x and y.
        =     Equality of x and y
     §^       to the power of
       *      x times y.
S≡           Does the result have the same shape and distribution of truthy values as A?

The idea is that Husk defines 0 to the power of 0 as 1, so the outer product has 1s on the first row and column. Also, 1 to the power of any number is 1, so the outer product has 1s on the diagonal. Other entries are 0 to the power of some positive number, which is 0. This gives a binary arrowhead matrix, which we compare to the input with .

Zgarb

Posted 2017-12-20T19:00:16.643

Reputation: 39 083

2

Clojure, 128 95 92 85 bytes

#(every? neg?(for[R[(range(count %))]i R j R]((if((set[i(- i j)j])0)- dec)((% i)j))))

It is always exciting to see two consecutive opening brackets.

Original version:

#(and(apply =(map assoc(for[i(rest %)](subvec i 1))(range)(repeat 0)))(every? pos?(concat(map nth %(range))(% 0)(map first %))))

The first part works by associng diagonal elements of the sub-matrix to zero, and checking that all rows are equal :) I used a similar trick at Jacobian method.

Latter part concatenates the diagonal + first row and column and checks that they are positive.

NikoNyrh

Posted 2017-12-20T19:00:16.643

Reputation: 2 361

2

Javascript (ES6), 58 bytes

My solution for Javascript:

m=>m.some((r,i)=>m[0][i]*r[0]*r[i]==0|r.filter(c=>i*c)[2])

Not as clever as Herman's answer, but I just felt like I should post it here too.

isArrowHead=m=>m.some((r,i)=>m[0][i]*r[0]*r[i]==0|r.filter(c=>i*c)[2])

console.log("-----should be false-----")

console.log(isArrowHead([[1, 1, 1], [1, 1, 0], [1, 0, 1]]))
console.log(isArrowHead([[1, 2, 3, 4], [1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]))
console.log(isArrowHead([[1, 2, 2, 2], [2, 1, 0, 0], [3, 0, 1, 0], [4, 0, 0, 1]]))
console.log(isArrowHead([[34, 11, 35, 5], [56, 567, 0, 0], [58, 0, 679, 0], [40, 0, 0, 7]]))

console.log("-----should be true-----")

console.log(isArrowHead([[3, 5, 6], [7, 1, 0], [8, 0, 0]]))
console.log(isArrowHead([[9, 9, 9, 9], [9, 9, 0, 0], [9, 7, 9, 0], [9, 0, 0, 9]]))
console.log(isArrowHead([[1, 0, 3, 4], [1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]))
console.log(isArrowHead([[1, 6, 3, 4], [13, 2, 0, 6], [29, 0, 1, 0], [2, 0, 0, 4]]))

console.log("-----end-----")

Pedro Corso

Posted 2017-12-20T19:00:16.643

Reputation: 121

3Welcome to PPCG! – Steadybox – 2017-12-22T16:20:20.283

2

Stax, 11 bytesCP437

ä¢⌠┐xⁿtH↔BU

Try it online!

Unpacked version with 13 bytes:

B|AsF:10i^\=*

Finally tied Husk and beaten by Jelly by just one byte ...

Explanation

B                Push tail (all except 1st row) of the input array, then push the head (1st row)
 |A              All elements in the head are truthy
                 This will be used as an accumulator
   sF            For each element in the tail, execute the rest of the program
     :1          All truthy indices
       0i^\      Expected truthy indices (0 and the current row number)
           =     The truthy indices are as expected
            *    Perform logical "and" with the accumulator
                 Implicit output of the final accumulator

Weijun Zhou

Posted 2017-12-20T19:00:16.643

Reputation: 3 396

1

R, 81 79 bytes

function(x){n=nrow(x);i=c(seq(1,n^2,n+1),1:n,seq(1,n^2,n));all(x[i]>0,x[-i]<1)}

-2 bytes thanks to Mr. Xcoder

Try it online!

duckmayr

Posted 2017-12-20T19:00:16.643

Reputation: 441

79 bytes. – Mr. Xcoder – 2017-12-20T19:50:43.083

Very nice; I managed to find a 78 byter that's doing something very odd but I also found a 76 byte golf of yours.

– Giuseppe – 2017-12-20T19:59:09.400

69 bytes but I improved mine to 68! – Giuseppe – 2017-12-20T21:31:37.437

1

PowerShell, 186 bytes

$a=$($args);if($a[0]-contains0){0;exit};0..($a.Length-1)|%{if($a[$_][0]-eq0-or$a[$_][$_]-eq0){0;exit};$r=$a[$_];$d=$_;if($_-ne0){1..($r.Length-1)|%{if($r[$_]-ne0-and$_-ne$d){0;exit}}}};1

Try it online!

root

Posted 2017-12-20T19:00:16.643

Reputation: 241

2Some golfs -- use param($a) to take input, the -contains can be swapped for an -in and all of the -eq0 can be swapped for !. Finally, you can loop from 1 up to $a.length and get rid of the if($_-ne0) in the loop body. – AdmBorkBork – 2017-12-20T21:24:41.787

1

C, 117 bytes

i,j,r;f(A,n)int*A;{for(i=r=0;i<n;++i)for(j=-1;++j<n;(!i||!j||i==j)&&!A[i*n+j]&&++r)i*j&&i-j&&A[i*n+j]&&++r;return!r;}

Try it online!

Steadybox

Posted 2017-12-20T19:00:16.643

Reputation: 15 798

1

Perl 5, 136 + 2 (-ap) = 138 bytes

push@a,[@F]}{push@b,"@{$a[0]}"=~/\b0\b/;map{//;map$a[$'][$_]=!$a[$'][$_],0,$';shift@{$a[$']};push@b,@{$a[$']}}1..$#a;say!("@b"=~y/ 0//c)

Try it online!

Xcali

Posted 2017-12-20T19:00:16.643

Reputation: 7 671

1

Clean, 79 bytes

import StdEnv
?[h:m]=prod h>0&&and[u>0&&n!!x>0&&sum n==n!!x\\[u:n]<-m&x<-[0..]]

Try it online!

Οurous

Posted 2017-12-20T19:00:16.643

Reputation: 7 916

1

Japt, 16 bytes

Ëe@!X^!(E*Y*nE
e

Test it online!

Man, this takes me back to the good old days when Japt was regularly much longer than other golfing langs...

ETHproductions

Posted 2017-12-20T19:00:16.643

Reputation: 47 880

1

K (oK), 27 30 bytes

Solution:

x~a*x|a:a|+(a:1,(#1_x)#0)|=#x:

Try it online!

Explanation:

I must be doing something dumb as the APL solutions are less than half the byte count...

24 bytes spent creating the arrowhead. or together the following three matrices:

/ assume 4x4 matrix
=#x
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

+(a:1,(#1_x)#0)
1 0 0 0
1 0 0 0
1 0 0 0
1 0 0 0

a
1 1 1 1
0 0 0 0
0 0 0 0
0 0 0 0

Full breakdown:

x~a*x|a:a|+(a:1,(#1_x)#0)|=#x: / the solution
                            x: / save input as x
                           #   / count length
                          =    / identity matrix
                         |     / or with
           (            )      / do this together
                      #0       / take from 0
                ( 1_x)         / drop first of x
                 #             / count
              1,               / prepend 1
            a:                 / save as a
          +                    / flip rows/cols
         |                     / or with
        a                      / a
      a:                       / save as a
     |                         / or with
    x                          / x
  a*                           / multiply by arrowhead
x~                             / matches input?

streetster

Posted 2017-12-20T19:00:16.643

Reputation: 3 635

1

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)

Kevin Cruijssen

Posted 2017-12-20T19:00:16.643

Reputation: 67 575

1ć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! – Mr. Xcoder – 2018-05-26T11:37:07.583

@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..) – Kevin Cruijssen – 2018-05-26T11:55:48.897

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 :) – Mr. Xcoder – 2018-05-26T11:58:38.077

@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!) – Kevin Cruijssen – 2018-05-26T12:04:14.427

Sure, that works. Have a nice weekend too! – Mr. Xcoder – 2018-05-26T12:05:06.530

Done, posted my own answer. – Mr. Xcoder – 2018-06-05T10:19:01.923

1

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.

Mr. Xcoder

Posted 2017-12-20T19:00:16.643

Reputation: 39 774

1

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!

JayCe

Posted 2017-12-20T19:00:16.643

Reputation: 2 655

0

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

l4m2

Posted 2017-12-20T19:00:16.643

Reputation: 5 985

@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 – l4m2 – 2017-12-20T20:02:34.100

0

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

raznagul

Posted 2017-12-20T19:00:16.643

Reputation: 424

0

PHP, 80 bytes

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

Arthur Shveida

Posted 2017-12-20T19:00:16.643

Reputation: 141

0

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 

RosLuP

Posted 2017-12-20T19:00:16.643

Reputation: 3 036

0

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;
}

Jakob

Posted 2017-12-20T19:00:16.643

Reputation: 2 428

0

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!

MegaTom

Posted 2017-12-20T19:00:16.643

Reputation: 3 787

0

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.

Conor O'Brien

Posted 2017-12-20T19:00:16.643

Reputation: 36 228