New answers tagged

1

C (gcc), 121 bytes l;e;r;o;a;f(m,n)int*m;{r=l=o=0;a=~0;for(e=1;r++<n;o|=*m,a&=*m++)~*m<<32-n?e&=!*m:++l;for(o-=a;a;a/=2)l+=a&1;r=l<2&&e^!o;} This has entire rows stored in integer arrays, making the maximum matrix size 32. Try it online!


1

GNU-APL, 34 bytes {+/(⍺≡+/,⍵)∧(⍵∧.=⍺⍴1)∨((⍺⍴1)∧.=⍵)} Input the size of the matrix, and the matrix itself. ⍵∧.=⍺⍴1 finds the row with all ones, a1=1∧a2=1∧a3=1 ... (⍺⍴1)∧.=⍵ finds the column with all ones The disjunction of the above two ensures atleast one of them happends ⍺≡+/,⍵ ensures that only required numbers of ones are present


1

PowerShell, 49 45 46 bytes inspired by Arnauld's and Neil's answers. +1 bytes with additional Test cases. "$args "-match'^(0*10* )\1+$|^(0+ )*1+ [0 ]*$' Try it online! convert the arguments to a string with space char as separator add the trail space char match with regexp


2

tinylisp, 88 bytes (load library (d _(q((M)(e 1(*(sum(map all M))(sum(map any M (q((M)(+(_ M)(_(transpose M The solution is the anonymous function in the last line. Try it online! Here's an ungolfed version that verifies all test cases: Try it online! Explanation Define a helper function _ that takes a matrix (list of lists) M: (sum(map all M)) ...


4

Python 2, 43 bytes Heavily based on dingledooper's answer, which you should upvote. lambda a,n:set(map(sum,a+zip(*a)))=={0,1,n} Try it online! Or: Python 3, 44 bytes: lambda a,n:{*map(sum,a+[*zip(*a)])}=={0,1,n} Saves bytes with set unpacking, but loses bytes because zip doesn't return a list. Try it online! Explanation In a truthy input, the sum of a row ...


1

APL (Dyalog Unicode), 63 55 bytes {(F⍉⍵)+(F←{(1↑⍴⍵)=(+/,⍵)×+/∧/⍵})⍵} Try it online! changed approach F used on input + input transposed ×+/∧/⍵ ⍝ number of all 1s lines multiplied by (+/,⍵) ⍝ total 1s (1↑⍴⍵)= ⍝ equal to side length?


2

T-SQL, 152 bytes Input is a table variable Returns 1 for true, 0 for false To save bytes, this can only handle matrix up to length 10 SELECT top 1iif(x=replace(a,0,'')or sum(x)over()=x*2-1and a not like'%0%'and 0=min(a)over(),1,0)FROM(SELECT rank()over(order by a)x,*FROM @)c ORDER BY-x Try it online


1

Charcoal, 27 bytes WS⊞υι∨∧⁼⌊υ⌈υ⁼¹Σ⌈υ⁼ΦυΣι⟦⭆⌈υ¹ Try it online! Link is to verbose version of code. Takes input as a newline-terminated list of strings. Explanation: WS⊞υι Input the matrix. ⁼⌊υ⌈υ All rows are identical ∧ Logical And Σ⌈υ The sum of a row ⁼¹ Equals literal `1` ∨ ...


3

Julia, 37 bytes x$n=(l=sum([x x'],dims=1))[l.>1]==[n] Try it online! uses Razetime's approach


6

05AB1E, 8 bytes Based on the first part of Bubblers Jelly answer, which uses dingledooper's algorithm. Input is a 2d matrix and the side length, output is 1 for truthy inputs and any other integer for falsy ones. This is the definition of truthiness in 05AB1E. Dø«O{¥θα Try it online! or Try all cases! D # push two copies of the input matrix ø ...


3

Retina 0.8.2, 32 bytes ^(0*10*)(¶\1)*$|^(0+¶)*1+(¶0+)*$ Try it online! Turns out to be much like @Arnauld's answer, except I wanted to solve it for no trailing newline instead.


10

JavaScript (ES6), 46 bytes Thanks to @PertinentDetail for a bug fix Expects a multiline string with a trailing line feed. Returns a Boolean value. s=>/^(0*10*\n)\1*$|^(0+\n)*1+\n[^1]*$/.test(s) Try it online!


6

Jelly, 9 bytes ŒṪZEƇȧiɗJ Try it online! The fourth totally different Jelly 9 byter. Returns either 1 or 2 for truthy and either an empty list or the number 0 for falsy. Also the program structure is more convoluted. How it works ŒṪZEƇȧiɗJ Monadic link. Input: the matrix M of size n ŒṪZ Take the transpose of multi-dimensional indices of 1 ...


11

Ruby, 47 bytes ->m,n{m.uniq[1]?m-[[0]*n]==[[1]*n]:m[0].sum==1} Try it online! Explain please: Split the two cases: If m has at least 2 different rows, remove all zeros and check if we are left with a single row consisting of ones. If not (all the rows are the same), check that the sum of the first row is one.


8

Wolfram Language (Mathematica), 21 13 35 bytes Norm@#==#2&&Norm@Eigenvectors@#>#2& Try It Online! (Thanks to @ZaMoC for configuring this.) Takes # as the matrix, and #2 as the square root of the side length, n. Returns True if the conditions are met, and False otherwise Explanation In Mathematica, the Norm function, when applied to a matrix, ...


13

Brachylog, 8 bytes {z|}≡ᵛ+1 Try it online! {z|} Either the rows or the columns ≡ᵛ are all the same list +1 which sums to 1.


5

Wolfram Language (Mathematica), 25 bytes #||#/.{a_..}:>Tr@a==1& Try it online! Returns True if the matrix has a single line, and nonTrue otherwise. #||# input OR its tranpose /.{a_..}:> replace lists of repeated elements with Tr@a==1 (is the sum of that element equal to 1?) Then, any Or ...


7

Vyxal, 12 bytes :∑$v∑Js¯Nt›= Try it Online! Yay! Beating Lyxal by 1 byte with a different approach!!! -1 byte from @Lyxal himself thanks


5

Jelly, 9 bytes ,ZfJṁ=ⱮJƲ Try it online! Idea stolen from Razetime in TNB , Pair the input with Z its transpose. f Filter the pair to elements contained in Jṁ=ⱮJƲ every matrix of the same dimensions with a single column of 1s. Jṁ Mold [1 .. len(input)] to the shape of the input. ⱮJ For each 1 .. len(...


8

Jelly, 9 bytes S;§ṢIṪ‘=L Try it online! Slightly modified port of dingledooper's algorithm. How it works S;§ṢIṪ‘=L Monadic link; input = matrix M S;§ Column sums of M ++ Row sums of M Ṣ Sort I Increments; (next - previous) for each adjacent pair Ṫ Last item ‘ +1 =L Equals the length of M? ...


10

Factor + math.unicode, 59 bytes [ [ concat Σ = ] keep dup flip [ all-equal? ] bi@ or and ] Try it online! Takes \$n\$ and a matrix of \$1\$s and \$0\$s, in that order. [ concat Σ = ] keep Is the sum of the matrix equal to \$n\$? dup flip [ all-equal? ] bi@ or and And are all the rows in either the original matrix or its transposition equal?


2

Vyxal, 14 bytes :ÞTJv∑s2Nȯ1?"⁼ Try it Online! Another stealing of the python answer. Explained :ÞTJv∑s2Nȯ1?"⁼ :ÞTJ # merge the input list with itself transposed. v∑s # sort the list that results from getting the sum of each item 2Nȯ # take the last two items of that list 1?"⁼ # and check for equality ...


6

J, 25 bytes (1:0}0"0)e.],&(#\|."{])|: Try it online! Not the shortest (my previous translation of dingledooper's was a couple bytes shorter), but I wanted to come up with a different approach: (1:0}0"0) Is the matrix with all ones in the first row and zeros elsewhere... e. an elment of... ],&(#\|."{])|: All possible rotations of ...


6

Jelly, 10 bytes S;§>Ƈ1⁼LW$ Try it online! Uses Unrelated string's test suite. Fixed with some help from Unrelated string. Explanation S;§>Ƈ1⁼LW$ S sums of columns ; concat with § sums of rows Ƈ keep the sums which are: > 1 greater than 1 ⁼LW$ = [length] ?


27

Python 2, 49 bytes Takes as input a 2D binary matrix \$ a \$, and its size \$ n \$. lambda a,n:sorted(map(sum,a+zip(*a)))[-2:]==[1,n] Try it online! There may be shorter approaches, but this is what I could find for now. Please let me know if the algorithm is incorrect. Explanation Since there is exactly one line, there should be exactly one row/column ...


2

Vyxal, 17 bytes £ḢṪṘƛ\a=[&h|&Ḣ];¥ Try it Online! A mess :P


Top 50 recent answers are included