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