When solving a decision-problem, a program takes an input and either accepts or rejects it. How is accepting and rejecting defined for the purposes of code golf?
2 Answers
In decision-problem challenges, solutions output one of two distinct outputs, depending on the properties of the input. "Accepting" and "rejecting" can be mapped to those two distinct outputs - one represents acceptance, and the other represents rejection. How the mapping is done depends on the challenge - if the challenge is of the "Does the input have property X" variety, then the "accepted" output is whichever output is given for inputs with the property, and the "rejected" output is the other one.
For challenges of the form "Is the input part of set X or set Y", it's less clear (especially if X and Y are not complements of each other). But, in those challenges, there's not really a need to distinguish "acceptance" from "rejection" (and if there is, the challenge can be rephrased as "Does the input belong to set X?").
The programmer chooses either to output a truthy/falsey value, or to crash or not crash
The programmer picks between two options:
- Their program always outputs a truthy/falsey value. If it corresponds to true, it has accepted the input. If it corresponds to false, it has rejected the input. (If it does not output a truthy value, it has neither accepted nor rejected the input, and, depending on the question, is probably incorrect.)
- Their program either terminates with no errors or with errors. If it terminates with no errors, it has accepted the input. If it terminates with errors, it has rejected the input. (If it does not terminate, it has neither accepted nor rejected the input, and, depending on the question, is probably incorrect.)
-
3\$\begingroup\$ "a truthy value [...] If it corresponds to false," What? \$\endgroup\$– DoorknobJun 1, 2017 at 22:51
-
-
\$\begingroup\$ Evaluating truthiness for an output is a bit weird. Let's say a program writes the character 0 to standard output. Is that truthy? In some languages, all non-empty strings are truthy. Or are we implicitly assuming there is some kind of type conversion and parsing of the output into some other type? Are we assuming that the text "0" should be read as an integer, and then evaluated, as if it was an expression in that same language? It's all pretty weird and murky. Is no output falsy? Is a newline with nothing else falsy? Etc. \$\endgroup\$ Jun 5, 2017 at 7:52