When challenges require to read a string via STDIN/Console I have seen people interpreting that in mainly two ways. The first one was that you actually just have to type the string as
This is an example string.
the second way required you to also type some sort of delimiter (usually double "
or single '
quotes)
"This is an example string."
or
'This is an example string.'
The question now is: Which one of those does actually count as string input via STDIN?
I am not sure, but I can imagine that there are languages where only one of those methods is possible for accepting strings as input.
On the other hand it might be easier (in codegolf: shorter) in some languages to use one or the other way.
2 Answers 2
Unless the challenge specifies, either is acceptable
Just be sure to mention what form of input your program requires in your explanation.
-
3\$\begingroup\$ I don't think it really matters; all it really comes down to is how your language accepts input. If they disallow certain types of input, then yeah, of course you can't use it, but otherwise, why not? \$\endgroup\$Addison Crump– Addison Crump2015年12月01日 12:05:10 +00:00Commented Dec 1, 2015 at 12:05
-
\$\begingroup\$ Is it correct that you mean as long as the OP does not explicitly say that the input cannot be assumed to be delimited, you can assume the string to be or not to be delimited if you want to submit a program that requires it to be either way? \$\endgroup\$flawr– flawr2015年12月01日 13:08:30 +00:00Commented Dec 1, 2015 at 13:08
-
\$\begingroup\$ @flawr this is exactly what I mean \$\endgroup\$quintopia– quintopia2015年12月01日 17:07:54 +00:00Commented Dec 1, 2015 at 17:07
Without quotes
I have always assumed that quotes weren't allowed.
In the ideal case, I think standard input should be the same for everyone and contain the challenge input as plainly as possible. This is how online judges work (e.g. codegolf.com), although I realise PPCG is a bit different.
Allowing stdin to be pre-formatted with quotes seems unnecessary, and will inevitably be abused. I'm not aware of any languages that read from stdin but strictly require quotes.
It's also worth pointing out that up to 541 existing Python 2 answers could be shortened by using input()
with quotes around each line in place of raw_input()
.
-
\$\begingroup\$ Abusing quotes in Haskell: you can use
readLn
instead ofgetLine
if the input is surrounded with"
. \$\endgroup\$nimi– nimi2015年12月01日 16:24:51 +00:00Commented Dec 1, 2015 at 16:24 -
\$\begingroup\$ Also, Mathematica could use
Input[]
instead ofInputString[]
. \$\endgroup\$LegionMammal978– LegionMammal9782015年12月01日 22:04:43 +00:00Commented Dec 1, 2015 at 22:04 -
\$\begingroup\$ I prefer this method because it avoids potential problems with the
"
characters needing to be in the input. I also think it can help with people trying to coerce the challenge into just being aneval(input())
sort of deal. \$\endgroup\$FryAmTheEggman– FryAmTheEggman2015年12月03日 01:16:35 +00:00Commented Dec 3, 2015 at 1:16 -
\$\begingroup\$ Python 2:
input()
accepts"double-quoted strings"
and'single-quoted strings'
as input, as opposed toraw_input()
, which acceptsnon-quoted strings
for +4 bytes. That's an example of why your answer is not good. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年07月04日 22:18:20 +00:00Commented Jul 4, 2016 at 22:18 -
1\$\begingroup\$ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ what? \$\endgroup\$grc– grc2016年07月05日 02:02:15 +00:00Commented Jul 5, 2016 at 2:02