Inspired by Wzl
Determine if a given square binary matrix has exactly one row or column consisting of entirely 1s, and the rest of the matrix is 0s.
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
1s outside the "all 1s column"
- There isn't a row or column of all
1s (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)
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 |
000000000 000000000 000000000 000000000 000000000 000000000 000000000 000000000 111111111
|
true |
00000 11111 00000 00000 00000
|
true |
1000 1000 1000 1000
|
true |
01 01
|
true |
0000000 1111111 0000000 1111111 0000000 0000000 0000000
|
false |
01000 11111 01000 01000 01000
|
false |
0100 1101 0100 0100
|
false |
1000 0100 0010 0001
|
false |
000 101 000
|
false |
000 000 000
|
false |
001 100 010
|
false |
01 00
|
false |
1101 0010 0000 0000. \$\endgroup\$/.sums, which produce a string of#ones and zero elsewhere iff valid input. But golfing that condition was cumbersome. Maybe you can make it work. \$\endgroup\$1100 1100 0000 0000– which suggests that it is the same algorithm as theorist's answer, that also fails with this case. :-) \$\endgroup\$