Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Is there a single straight line in the matrix?

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

Answer*

Draft saved
Draft discarded
Cancel
7
  • \$\begingroup\$ Fails with inputs like 1101 0010 0000 0000. \$\endgroup\$ Commented Jun 17, 2021 at 4:35
  • \$\begingroup\$ Ugh, nice catch. \$\endgroup\$ Commented Jun 17, 2021 at 4:35
  • \$\begingroup\$ @Bubbler Fixed. I really wanted to find a solution with using diagonal /. 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\$ Commented Jun 17, 2021 at 5:00
  • \$\begingroup\$ Why do you need to square element wise? The matrix always has 0 or 1 in it, and squaring either of those (or any product involving those) does nothing \$\endgroup\$ Commented Jun 17, 2021 at 13:56
  • 2
    \$\begingroup\$ Fails with 1100 1100 0000 0000 – which suggests that it is the same algorithm as theorist's answer, that also fails with this case. :-) \$\endgroup\$ Commented Jun 17, 2021 at 15:16

AltStyle によって変換されたページ (->オリジナル) /