Is this string a square?
Python 2, 52 bytes
x=input().split('\n')
print{len(x)}==set(map(len,x))
Try it online! or Try all test cases
Brachylog (2), 3 bytes
ṇẹṁ
Try it online!
Full program. Outputs true.
for truthy, false.
for falsey.
Explanation
ṇẹṁ
ṇ Split {standard input} into lines
ẹ Split {each line} into basic elements {in this case, characters}
ṁ Assert that the result is square
I was a bit sceptical about the usefulness of the ṁ
builtin when it was added, but I can't really deny that it's helpful here…
Brachylog (2), 7 bytes
ṇẹ.\l~l
Try it online!
Non-builtin solution. Still beats all the other entries, as of the time of writing. EDIT: Not quite, the equal-length Jelly entry got in while I was writing this, and beats it via the timestamp tiebreak.
Explanation
ṇẹ.\l~l
ṇ Split {standard input} into lines
ẹ Split {each line} into basic elements {in this case, characters}
\l Assert that the result is rectangular, and the number of columns
. ~l is equal to the number of rows
JavaScript (ES6), 46 45 bytes
s=>!(s=s.split`
`).some(x=>x.length-s.length)
- 1 byte saved thank to ETHproductions
Explanation
- Split the string to an array on newlines.
- Loop over the array.
- Subtract the length of the array from the length of each line.
- If a non-zero (i.e., truthy) value is returned for any line, the string is not square.
- Negate the result of the loop to get
true
for square andfalse
for not.
Try it
f=
s=>!(s=s.split`
`).some(x=>x.length-s.length)
oninput=_=>o.innerText=f(i.value)
o.innerText=f(i.value=`foo
bar
baz`)
<textarea id=i></textarea><pre id=o>