Determine if a given square binary matrix has exactly one row or column consisting of entirely 1
s, and the rest of the matrix is 0
s.
For example, these are all true:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1
And these are all false:
0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0
1 1 1 1 1 1 1 0 1 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
The reasons for the false ones:
- More than one row
- More than one row or column
- There are
1
s outside the "all1
s column" - There isn't a row or column of all
1
s (describes the last 4)
You should take a square binary matrix of side length \$n \ge 2\$, in any reasonable format, and output 2 distinct, consistent values to indicate whether it has a single straight line. This is code-golf, so the shortest code in bytes wins.
Keep your golfing in your code, not your inputs/outputs. I'm very liberal with the I/O formats, including but not limited to:
- A multiline string
- A list of lines
- A 2D list (or matrix) consisting of two distinct consistent values (e.g.
0
/1
, space/'*'
,5
/'('
) - A flat array, and \$n\$, as separate arguments
- Two distinct consistent values (e.g.
1
/0
) to indicate the output - Erroring/not erroring
- Two families of values, one of which consists of values that are truthy in your language and the other falsey (for example, natural numbers and zero)
- etc.
As a broad, general rule, so long as the input can be clearly understood and the outputs clearly distinguished, it's fair game.
Test cases
Input | Output |
---|---|
|
true |
|
true |
|
true |
|
true |
|
false |
|
false |
|
false |
|
false |
|
false |
|
false |
|
false |
|
false |