Fixed output challenges usually state that the program "must take no input". I usually interpret this as "The standard input is empty", but after thinking about this, there are actually several valid interpretations:
- The program has to ignore the standard input, that is, it should work regardless of what is given as input.
- The program has to work only when an empty stream is piped into the standard input.
- The program has to work without piping anything into it. It may block for user input, but can expect the input to be empty.
- The program has to work without piping anything into it and it must not block for user input.
There are probably even more interpretations. The last three are worth distinguishing: for instance, my own language Retina was until recently not able to prevent blocking for user input when nothing was piped in - so to really run it without any input one had to do
echo "" | ./Retina -s program.ret
I imagine there are other languages which have this limitation.
So what should the default meaning of "your program must take no input" be?
(There was a related discussion for languages which plainly incapable of executing any code at all if no input is given.)
2 Answers 2
Programs may assume that input is empty
I believe that the requirement
The program has to ignore the standard input, that is, it should work regardless of what is given as input.
is too strong. There are (mostly esoteric) languages which automatically read all input there is. These languages would always have to include some code to get rid of that input first, which doesn't really add anything to these submissions (except bytes, of course...).
Hence, programs should generally be allowed to expect that the input is completely empty. Any setup that is necessary for the input to be empty may be expected as well. That is, if a language can only read an empty input from a pipe (as used to be the case with Retina), such an empty pipe can be expected. If a language cannot suppress a user prompt, the solution may expect this prompt to return empty. If a language can only read from a file (for whatever reason), it may expect that file to exist and be empty.
This rule is intended to be as inclusive and fair as possible. I can't see this opening any loopholes, but let me know if you can think of any.
Of course, as usual, any challenge author is free to override this default if their particular challenge becomes more interesting by requiring the code to work for any input (in which case they should clarify whether input can be assumed to be finite).
-
\$\begingroup\$ Question: Let's say your program/function can work without arguments, but taking an empty argument is shorter for the specific language. Can we in that case take an empty argument when none is expected (which we don't use anywhere, we only use it to shorten the byte-count), or would this be disallowed? (I.e. Java lambda function
e->
vs()->
(e
= an empty argument we don't use in the program, like""
ornull
; and()
is no arguments at all). \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年06月01日 12:38:40 +00:00Commented Jun 1, 2017 at 12:38 -
1\$\begingroup\$ @KevinCruijssen This post was specifically for programs. I don't know if we have a corresponding post for functions somewhere, but if not it might be worth writing one. \$\endgroup\$2017年06月01日 12:50:53 +00:00Commented Jun 1, 2017 at 12:50
-
1\$\begingroup\$ Ok, I've created a new meta-post with this question. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年06月01日 14:49:43 +00:00Commented Jun 1, 2017 at 14:49
-
\$\begingroup\$ To clarify on this, I'm assuming code like
perl -p
shouldn't be assumed to allow empty input too? Allowing so would help in many ways when we have to avoid callingprint
directly as we can rely on the default behaviour toprint
, or easily callredo
as we're within{
...}
? I've never really considered that valid, but this and Kevin's question has made me think to clarify. \$\endgroup\$Dom Hastings– Dom Hastings2017年06月09日 07:38:07 +00:00Commented Jun 9, 2017 at 7:38 -
\$\begingroup\$ @DomHastings sorry, not quite sure I'm following (because my Perl knowledge is fairly limited). Could you give me some TIO links for a program that you think could abuse this rule (and what it would normally look like)? \$\endgroup\$2017年06月09日 08:11:36 +00:00Commented Jun 9, 2017 at 8:11
-
\$\begingroup\$ So it seems that TIO and ideone both ignore output, which is confusing for me, but using the binary on my machine I can run:
perl -pe 's//Hello, World!/' <<< ''
instead ofperl -e 'print"Hello, World!"'
for -2 bytes. If used without empty input, it would hang for user input. \$\endgroup\$Dom Hastings– Dom Hastings2017年06月09日 08:24:11 +00:00Commented Jun 9, 2017 at 8:24 -
\$\begingroup\$ Interestingly, putting a null byte in the input does work on TIO. (Without putting a null byte in the output) \$\endgroup\$Dom Hastings– Dom Hastings2017年06月09日 08:31:52 +00:00Commented Jun 9, 2017 at 8:31
-
1\$\begingroup\$ @DomHastings ah I see. Well that's the same problem Retina used to have. It would always expect there to be some input, so if you didn't pipe in an empty input, it would wait for user input. I agree that it's a bit weird to allow this in languages like Perl that can work without any input whatsoever, but I'm not a fan of language-specific rules, so I think it should be fine for all languages to assume that they're being passed an empty input stream. \$\endgroup\$2017年06月09日 08:49:44 +00:00Commented Jun 9, 2017 at 8:49
-
\$\begingroup\$ Thanks for the clarification, I'll see if this helps me in any future solves! \$\endgroup\$Dom Hastings– Dom Hastings2017年06月09日 08:57:45 +00:00Commented Jun 9, 2017 at 8:57
-
\$\begingroup\$ In
awk
when a program has onlyBEGIN
block, it reads no input. A shorter version with onlyEND
block behaves the same, except it waits for input (ignoring it). Is it acceptable? \$\endgroup\$mik– mik2021年03月15日 15:52:54 +00:00Commented Mar 15, 2021 at 15:52 -
\$\begingroup\$
There are [...] languages which automatically read all input there is. These languages would always have to include some code to get rid of that input first, [...]
Well, tough titties. There is a remarkable amount of accommodations for golfing languages, but standard languages don’t see the same amount of consideration. I’ll therefore repeat the same recommendation others expressed: You can always choose a different language. For some challenges automatically pushing standard input on the stack can be advantageous. For other challenges this can be annoying. \$\endgroup\$Kai Burghardt– Kai Burghardt2023年09月05日 18:14:01 +00:00Commented Sep 5, 2023 at 18:14
Programs must not depend on standard input
Take no input means that the program must not depend on standard input.
- It is not guaranteed that standard input is open for reading, nor that it is attached to a terminal, bound to a regular file, a socket or anything.
Your program must run and fulfill the task’s specifications regardless../myProgram <&- # the `<&-` _closes_ standard input
- If it is open for reading, it is not guaranteed that standard input is indeed empty. Your program must not depend on presence or absence of any data in standard input. Standard input may be filled with junk.
- The state of standard input at the point in time the execution of your submitted code commences must be left untouched until the end of your code. This accounts for the hidden preamble and/or epilog some programming languages/implementations exhibit. E. g. an automatic "flush" (discarding all input) at program termination is harmless.
Rationale
A program can be modeled as a black box or as a mathematical function \$f\$. Such a function \$f\$ takes some inputs and returns some output(s). If there is a "receptacle" for (standard) input, this black box’s behavior could depend on it. Such behavior is banned by the take no input rule. (A dummy receptacle, something that is not "wired", not connected, is harmless.)
Future directions
A better wording for future challenges might be:
Resources external to the execution environment are unavailable.
The execution environment refers to the black box’s interior. Unavailable indicates, while resources may exist you cannot "touch" them.
echo "" | java -jar cjam.jar program.cjam
still work if""
was not an empty string? I don't think the question makes sense in general. \$\endgroup\$1
you'd probably need to start off with;
(but whether that's actually the case, depends on the specific program - something else in the program might already get rid of the input that's already on the stack). As a side note, the program only ever works for a finite input stream (the same would go for Retina). \$\endgroup\$