25
3
Your task is to build a program that identifies the shape of the input. The shapes to be identified can be any of the following:
Square
To be identified as a square, the source must have lines of all equal length, and the same number of lines as characters per line (newline characters excluded). An optional trailing newline is acceptable.
$_='
$_="
$_"'
;say
Rectangle
To be identified as a rectangle, the source must have lines of all equal length, but the number of lines does not match the number of characters per line (newline characters excluded). An optional trailing newline is acceptable. This can be either horizontal or vertical.
$_=
"no
t a
squ
are
";#
$_="but it
is still a
consistent
shape!";##
Triangle
To be identified as a triangle, the source must either start with one character, and each subsequent line must have one additional character (including the last), or after the first line, each subsequent line should have one character fewer until the last, which has only one.
$
_=
"So
this
"."".
shape;
$_="or
even,
this
way
!!
"
Mess
Anything that doesn't follow a consistent format as per the above, must be identified as a mess.
Rules
- You may return any four consistent printable values to identify each shape.
- Your source code must also adhere to one of the above shapes (no, not a mess).
- A single trailing newline in your source is acceptable.
- You can assume input does not contain any blank lines (including trailing newlines), is not empty, and does not consist only of newlines.
- All shapes must have a height and width of >= 2, otherwise this is defined as a mess.
- Standard loopholes are forbidden.
- The shortest solution in bytes, in each language, wins.
"Your source code must also adhere to one of the above shapes" does it mean one liner is just fine? – tsh – 2018-03-23T08:44:29.633
1@ tsh
All shapes must have a height and width of >= 2.
– TFeld – 2018-03-23T08:45:12.940Is leading / trailing space count to the line length? What about tab? – tsh – 2018-03-23T08:47:37.213
@tsh Yes, trailing spaces do indeed count towards the length, tab is one character as if the source was in a file. – Dom Hastings – 2018-03-23T08:56:39.613
Do we have to confirm that a rectangle, say, has length and width at least 2, or is that just a restriction on our code? – xnor – 2018-03-23T10:09:41.610
@xnor Yes, you are correct that should be validated, otherwise it's a mess. I haven't actually tested that against these answers though... – Dom Hastings – 2018-03-23T10:24:04.140
When you say our code doesn't have to cater for trailing newlines, can we assume the input does not have one, or do we have to call it a mess? Might there be blank lines? Could the input be completely blank, or only consist of newlines? – xnor – 2018-03-23T10:39:11.527
@xnor You can assume input does not contain trailing newlines, blank lines, is empty, or consists only of newlines. Updating the question. – Dom Hastings – 2018-03-23T10:41:44.500
You might want to change the sentence "All shapes must have a height and width of >= 2." to "All shapes must have a height and width of >= 2, otherwise it counts as a Mess as well.". Or maybe explicitly add "Single character inputs should result in a Mess." – Kevin Cruijssen – 2018-03-23T11:14:48.457
1The input can be an array? for example, a square
['abc','cfd','fgh']
? – Luis felipe De jesus Munoz – 2018-03-23T12:23:16.947@LuisfelipeDejesusMunoz I think an array for strings might be standard input, but I can't find a meta post to confirm it. I think until I find a relevant meta, if that is the default in your language, that's fine. – Dom Hastings – 2018-03-23T13:31:46.377
Is prepending
using System.Linq;
to the beginning of the otherwise triangular source code fine? – my pronoun is monicareinstate – 2018-03-23T14:57:15.293IMO there are some confusing nested negatives. I'd suggest rewording to "You can assume input does not contain any blank lines (including trailing newlines), is not empty, and does not consist only of newlines." – recursive – 2018-03-23T16:19:00.350
@someone I want to say yes, just so there is more triangular code, but other answers wouldn't be able to validate your code as triangular. If there's a command-line option to provide that
using
argument, you can sayC# with Linq
, but a quick search doesn't show anything like that :( – Dom Hastings – 2018-03-23T17:02:08.8431@recursive updated, thank you! – Dom Hastings – 2018-03-23T17:02:34.523
What if the input is like the two triangle examples next to each other vertically? That is still triangle-shaped. – mbomb007 – 2018-03-23T18:21:15.537
@mbomb007 Not for the purposes of this challenge though... but a good point! – Dom Hastings – 2018-03-23T18:22:06.817
3You are telling me my source code can't be a mess? why not?!?! – NH. – 2018-03-23T23:31:06.860