44
6
A string is considered to be square if the following conditions are met:
- Each line has the same number of characters
- The number of characters on each line is equal to the number of lines.
Your task is to write a program or function which determines whether or not a given input string is a square.
You may require input to be delimited by your choice of LF, CR, or CRLF.
The newline character(s) are not considered part of the line's length.
You may require there to be or to not be a trailing newline in input, which doesn't count as an additional line.
Input is a string or 1D char array; it is not a list of strings.
You may assume input is non-empty and only contains printable ASCII, including spaces.
You must output a truthy value for square strings and a falsy one for other strings.
Truthy test cases:
foo bar baz
.
.s. .ss .s. (s represents space)
ss ss (s represents space)
aaaaa aaaaa aaaaa aaaaa aaaaa
Falsy test cases:
.. .
.
.
.... ....
4444 333 22
333 333
abc.def.ghi
Note extra blank lines in some of the falsy cases.
This is code-golf - fewest bytes wins!
Possible duplicate of Language Design: 2-D Pattern Matching. Problem #5 is the same as this question.
– mbomb007 – 2017-06-06T21:40:10.1701@mbomb007 I feel like the different winning criteria make this not a duplicate? "Golfiness" was one of the voting criteria but I don't think answers to that question will largely reflect on the ones here. – FryAmTheEggman – 2017-06-06T22:12:36.953
2@mbomb007 I'm voting to leave this question open because, while it is a subset of the other question, the other question is restricted to languages created specifically for that question. – ETHproductions – 2017-06-06T22:15:13.490
2@mbomb007: That's not a duplicate, because that question asks you to design a language for the purpose of answering the question, rather than answering in an existing language. Very few of the answers here would be legal there. – None – 2017-06-06T22:29:10.730
The solutions there are legal here, however. Many of the answers have an exact solution to this challenge contained in them. – mbomb007 – 2017-06-07T01:03:41.227
1@mbomb007: That's no reason to close this challenge, and give people nowhere to post their answers in pre-existing languages, though. It might potentially be an argument for closing the other challenge (because it's just a more restrictive version of this one), although I'd consider it a poor argument and believe both should be left open. – None – 2017-06-07T17:47:45.217
Technically, the first requirement is redundant as it can never be false if the second is true. – Antti29 – 2017-06-09T09:13:25.913