87
\$\begingroup\$

It looks like we have a consensus that we want certain defaults for the format which answers are expected in for . On that poll, the question arose twice, which input/output formats should be allowed for programs and functions.

So here is another poll. This one works different though. All the input/output methods are independent of each other, so there will be one answer per method. Upvote all you think are reasonable for the default. Downvote those which you think shouldn't be allowed unless the OP explicitly permits them.

To keep this remotely manageable, I have not posted individual answers for all possible inputs for functions. So there are only four: functions can take input/output via their arguments and return values. Or functions can use any method full programs can. I don't think there is any point in (say) allowing programs to take input from STDIN (only) and to allow functions to take input from ARGV (only) or something like that. If you disagree, please leave a comment.

If I've overlooked an I/O method, feel free to add your own answer.

Note: Some votes have been reverted because they were detected as serial voting. If you vote on multiple answers, please leave some time between votes.

A method is allowed if it has 5 net votes and at least twice as many upvotes as downvotes.

Update

The current results of the polls are now part of the tag wiki. Please notify me, if results change significantly and the wiki should be amended.

\$\endgroup\$
  • \$\begingroup\$ Related (Can numeric input/output be in the form of byte values) \$\endgroup\$ – mbomb007 Dec 13 '16 at 16:10
  • 1
    \$\begingroup\$ "The current results of the polls are now part of the tag wiki" which tag wiki? meta wiki has nothing for code-golf tag. ppcg wiki has codegolf.stackexchange.com/tags/code-golf/info but it's missing a lot of answers here. \$\endgroup\$ – Sparr Jul 18 '18 at 18:17

76 Answers 76

20
\$\begingroup\$

Regexes may output via the list of matched strings (the captured group 0)

\$\endgroup\$
  • 2
    \$\begingroup\$ We now have Retina for regex so this can \$\endgroup\$ – CalculatorFeline Apr 2 '16 at 16:13
  • \$\begingroup\$ @CalculatorFeline Retina is actually Turing Complete. \$\endgroup\$ – mbomb007 Feb 17 '17 at 19:14
  • \$\begingroup\$ ...I'm confused. \$\endgroup\$ – CalculatorFeline Feb 20 '17 at 23:34
  • 2
    \$\begingroup\$ @CalculatorFeline: Retina is more powerful than plain regex is, due to the way it redefined the backquote character (which just matches itself literally in most regex syntaxes). That's enough to make it Turing complete. \$\endgroup\$ – user62131 Apr 27 '17 at 15:56
  • \$\begingroup\$ True. But Retina is basically regex+. Do we need this anymore? \$\endgroup\$ – CalculatorFeline Apr 28 '17 at 21:59
  • \$\begingroup\$ I don't think using plain regex is even allowed, since it isn't Turing complete. \$\endgroup\$ – 12Me21 Mar 2 '18 at 2:22
19
\$\begingroup\$

CGI scripts may take input via GET/POST parameters

A CGI script required to take, e.g., three inputs can be expected to be called as the GET request

GET script.cgi?<A>=<1st input>&<B>=<2nd input>&<C>=<3rd input> HTTP/1.1

(where A, B and C are keys of the poster's choice) of the equivalent POST request.

\$\endgroup\$
  • \$\begingroup\$ Also, PHP<5.4.0 (exclude) has the directive register_globals (enabled by default up to PHP4.1), which allows you to use POST, GET, COOKIE, SESSION and what-not. It creates the variables with the names of the keys (E.g.: a.php?a=1 would create $_GET[a] (version-dependent) and the variable $a with value 1). This was removed due to security reasons, but was a standard feature that was (fortunally) removed. \$\endgroup\$ – Ismael Miguel Nov 27 '15 at 17:52
  • \$\begingroup\$ Also, would parse_str($_SERVER[QUERY_STRING]) be a valid input method? It does exactly the same as register_globals, but reads input parameters from a superglobal and parse them into variables. This is explained in the documentation: php.net/manual/en/function.parse-str.php \$\endgroup\$ – Ismael Miguel Nov 27 '15 at 18:01
  • \$\begingroup\$ This proposed a specific input method. What language features you use to access that input is up to you. \$\endgroup\$ – Dennis Nov 27 '15 at 18:20
  • \$\begingroup\$ Then using $_GET (superglobal with all GET parameters, in PHP) fits the "What language features you use to access that input is up to you", right? \$\endgroup\$ – Ismael Miguel Nov 27 '15 at 18:25
  • \$\begingroup\$ Yes, it does, assuming a GET request becomes an accepted input input. \$\endgroup\$ – Dennis Nov 27 '15 at 18:37
  • \$\begingroup\$ Then there is the program <?php eval(key($_GET));. (SQL table input may have similar problems.) \$\endgroup\$ – jimmy23013 Nov 27 '15 at 18:51
  • 1
    \$\begingroup\$ @jimmy23013 PHP4.1: <?eval($c); (assuming short_tags and register_globals, the code can be anywhere, even in a cookie (if below 4kb)). Or even <?eval(fgets(STDIN)). Or, for Javascript, eval(prompt()). Or eval "$s" for bash. Other languages have the same problem. Any language that allows evaluation of a string containing code has the same problem \$\endgroup\$ – Ismael Miguel Nov 27 '15 at 20:19
  • \$\begingroup\$ @Dennis Would eval(`return {${location.hash}}`), assuming your URL is http://example.com/#a:"value",other:"value", fit in this definition? \$\endgroup\$ – Ismael Miguel Nov 27 '15 at 20:32
  • \$\begingroup\$ @IsmaelMiguel As written, no. Those are neither GET nor POST parameters. \$\endgroup\$ – Dennis Nov 27 '15 at 20:34
  • \$\begingroup\$ @Dennis What about eval(location.search.replace(/^\?/,''))? It would create every single variable you would need, right away (if the input is numeric and not an array). The location.search will contain all the GET parameters, optionally starting with an ?. \$\endgroup\$ – Ismael Miguel Nov 27 '15 at 20:38
  • \$\begingroup\$ This can also be done with php -r '<code>' <A>=<input1> <B>=<input2> (or php-cli, depending on your distribution/installation), which alleviates the need for a web server (a much better option IMO). In this case, scoring is much easier: the -r flag would cost 2 bytes, but the ability to drop the leading <? in the code balances it out. Each GET parameter would cost 1 byte (for the =) plus the length of the parameter name. \$\endgroup\$ – Mego Nov 28 '15 at 6:11
  • \$\begingroup\$ @Mego The -r is free. Read: meta.codegolf.stackexchange.com/q/2424/14732 \$\endgroup\$ – Ismael Miguel Nov 28 '15 at 22:44
  • \$\begingroup\$ @IsmaelMiguel Fair enough. The rest of my comment still stands. \$\endgroup\$ – Mego Nov 28 '15 at 23:42
  • \$\begingroup\$ @Mego It's still under discussion. \$\endgroup\$ – Ismael Miguel Nov 29 '15 at 1:09
  • \$\begingroup\$ @IsmaelMiguel Hence why I offered my opinion \$\endgroup\$ – Mego Nov 29 '15 at 1:58
19
\$\begingroup\$

Input and output of a single value may be represented as a singleton list

For example, if the value was 4, [4] would be an acceptable representation (using Python list syntax).

\$\endgroup\$
  • 3
    \$\begingroup\$ This is useful in array languages like J. \$\endgroup\$ – Esolanging Fruit Apr 26 '17 at 16:51
19
\$\begingroup\$

An array may be represented in input as a pointer and length.

For example in C you could use:

int f(int*p,int l){...}
\$\endgroup\$
  • \$\begingroup\$ Do you think it should be restricted to C? \$\endgroup\$ – Keyu Gan Jul 12 '17 at 3:46
  • 2
    \$\begingroup\$ @KeyuGan I think it should be allowed in any language with pointers (c++, asm ...). Do you know of a language where this would be a problem? \$\endgroup\$ – MegaTom Jul 12 '17 at 13:25
  • 2
    \$\begingroup\$ I don't know whether a language exists where both pointer and array are well-defined. Under such circumstances, it may benefit a lot from this rule. I would still suggest languages which does not regard an array of general type as a first-class object may use a reference to first element and length to replace with array/list inputs. \$\endgroup\$ – Keyu Gan Jul 14 '17 at 9:12
  • 8
    \$\begingroup\$ I'd say this should only be for a language where this is the natural representation of an array \$\endgroup\$ – Justin Jul 19 '17 at 20:15
  • \$\begingroup\$ @keyugan C# supports both standard arrays and C-style pointers \$\endgroup\$ – Pavel Nov 8 '17 at 23:51
  • \$\begingroup\$ @Pavel It is the same situation with C++. As for C# (I'm not an expert), many answers are based on LINQ and seemingly they cannot be shortened in this way. Besides, I will say yes to use pointer / length encapsule here. I think it will be better if we could give some restrictions on what language could do so, according to meta discussion, while the original answer doesn't. codegolf.meta.stackexchange.com/questions/13210/… \$\endgroup\$ – Keyu Gan Nov 9 '17 at 9:17
  • \$\begingroup\$ @KeyuGan It's just a technicallity really, the only concievable reason to use pointer types in C# is for interop with native code. It probably won't ever apply. \$\endgroup\$ – Pavel Nov 9 '17 at 15:06
  • \$\begingroup\$ @KeyuGan Please don't make drastic changes to suggestions. \$\endgroup\$ – Mego Nov 12 '17 at 4:56
18
\$\begingroup\$

Functions may return their result in an optional type wrapper

Relevant meta discussion.

By "optional type wrapper" I mean something like Maybe a in Haskell, Option<T> in Rust, etc.

The consensus was that this should be allowed because it functions like a singleton wrapper, or like a possibly-erroring function that never actually errors.

\$\endgroup\$
16
\$\begingroup\$

black-box functions to be taken as input can be passed as an argument

This holds for functional languages or languanges that do have first class functions/anonymous functions/lambda functions or function pointers, for instance: Java, JavaScript, C++, TI-Basic, Python, Octave, Matlab, Haskell, Lisp

\$\endgroup\$
  • \$\begingroup\$ Huh? I'm confused what makes this different from passing inputs as arguments in general. \$\endgroup\$ – Solomon Ucko Sep 30 '18 at 3:24
  • \$\begingroup\$ @SolomonUcko You mean as opposed to other non-function input types? In that case nothing, but this should just serve as an additional clarification as we introduced black-box functions just last year into our rules. Also consider these two entries: codegolf.meta.stackexchange.com/a/13713/24877 codegolf.meta.stackexchange.com/a/13707/24877 \$\endgroup\$ – flawr Sep 30 '18 at 12:07
15
\$\begingroup\$

Programs may take input from a file

\$\endgroup\$
  • 9
    \$\begingroup\$ ... only if makes sense for the challenge, and even then you should input a file name first... I'd say \$\endgroup\$ – John Dvorak Nov 3 '14 at 6:32
  • 5
    \$\begingroup\$ @JanDvorak That's a "no", because that makes the file name itself the input (which you would take from STDIN or ARGV). \$\endgroup\$ – Martin Ender Nov 3 '14 at 11:07
15
\$\begingroup\$

Submissions may use list of strings instead of multi-line strings

Input or output may be the equivalent of the string separated by newlines. For example, the multiline string:

1
2
Fizz
4
Buzz

May be instead be taken as a list of strings like:

["1","2","Fizz","4","Buzz"]
\$\endgroup\$
  • 1
    \$\begingroup\$ I always see comments about this in multiline challenges, so I thought I'd throw this up there and see if it is well-recieved as a standard form of input \$\endgroup\$ – Jo King Nov 13 '18 at 11:54
  • 8
    \$\begingroup\$ I would add an exception for ascii-art as linebreaks can be an important part of those challenges. \$\endgroup\$ – lirtosiast Nov 19 '18 at 19:43
15
\$\begingroup\$

I/O for questions may either be 0-indexed or 1-indexed

Any question that has the user output the \$n\$th term, first \$n\$ terms, etc. of a sequence should allow programs to index the first element of the sequence as 0 or 1 at the answerer's choice, as long as the program is consistent.

\$\endgroup\$
  • \$\begingroup\$ This is now allowed by the sequence tag default. \$\endgroup\$ – lirtosiast 3 hours ago
14
\$\begingroup\$

Functions may return a function that returns the result

This is very similar to currying, the only difference being that only the first call includes an argument. Sometimes it's useful to curry a recursive function that has only one argument, especially if that argument doesn't need to be modified to find the result. For example, each of these JavaScript functions returns the bit-length of a positive integer:

f=(n,x)=>n?f(n>>1,-~x):x
f=(n,x)=>n>>x?f(n,-~x):x
n=>f=x=>n>>x?f(-~x):x

All three functions can be called by assigning them to g and calling them with e.g. g(49); however, the third version must be called again with g(49)() to actually return the result. Again, this is very similar to currying, where the arguments are passed separately (g(a)(b) rather than g(a,b)).

Note that no additional input should be needed to get the result (i.e. "to call on a number x, use f(x)(2)"). Optional arguments are fine, of course (x=>(y=2)=>x+y).

There is also a meta post about this here, though it was closed as a duplicate of this question.

\$\endgroup\$
  • 2
    \$\begingroup\$ The only restriction I'd like to see on this is that no additional input should be needed to fetch the result. In other words, the returned function should take no mandatory arguments (optional arguments are fine), so that f(whatever)() would return the result. Otherwise, it falls under additional unspecified input, and therefore violates that loophole. \$\endgroup\$ – Mego Mar 20 '17 at 4:12
  • \$\begingroup\$ @Mego Thanks, I agree. I thought that was implicit, but I've added a mention to the post. \$\endgroup\$ – ETHproductions Mar 20 '17 at 16:36
  • \$\begingroup\$ How many brackets at the last is allowed? 1? or allowed if not that large(only avoid loophole) \$\endgroup\$ – l4m2 Dec 20 '17 at 2:02
  • \$\begingroup\$ @l4m2 I'm only vouching for one extra function call here; you could write another answer allowing multiple extraneous calls if you wish. \$\endgroup\$ – ETHproductions Dec 21 '17 at 15:31
  • \$\begingroup\$ @l4m2, I thought about any constant (not related to input) number. \$\endgroup\$ – Qwertiy May 25 at 22:21
12
\$\begingroup\$

Functions may return a promise containing the output

About Promises


Example

Consider the following function to retrieve my rep from the API, with the output logged from the last Promise in the chain:

f=

_=>fetch("//api.stackexchange.com/users/58974?site=codegolf").then(r=>r.json()).then(j=>console.log(j.items[0].reputation))

f()

I propose instead that we simply be allowed to return the Promise containing the output, like so:

f=

_=>fetch("//api.stackexchange.com/users/58974?site=codegolf").then(r=>r.json()).then(j=>j.items[0].reputation)

f().then(console.log)


Conditions of Use

This section requires more detail, please edit it to include any other conditions you think should be covered or to add tags to the existing conditions.

In challenges that use code length as a measure for the winning criterion, returning a promise would not be permitted if further manipulation would be required to achieve the desired output, as in this example:

f=

_=>fetch("//api.stackexchange.com/users/58974?site=codegolf").then(r=>r.json()).then(j=>j.items[0])

f().then(i=>console.log(i.reputation))

If this method is used in time-based challenges then the time required to resolve the final promise must be counted towards your score.

\$\endgroup\$
  • \$\begingroup\$ I feel like we should allow it but add a penality of 5 bytes (.then) due to the overhead in calling like using a command line flag. \$\endgroup\$ – Christoph May 12 '17 at 6:34
  • \$\begingroup\$ @Christoph, I can see your point. I suggest posting that as a counter-proposal as simply editing in as an option above won't tell us what the consensus is. \$\endgroup\$ – Shaggy May 12 '17 at 7:11
  • \$\begingroup\$ I'd allow this except in fastest-code and similar time-based challenges (or at least count the time taken in forcing the promise, in that case), for what I hope are obvious reasons. \$\endgroup\$ – user62131 May 17 '17 at 7:23
  • \$\begingroup\$ @ais523, I've added a section on conditions of use, feel free to edit it to add more detail, tags and/or examples. \$\endgroup\$ – Shaggy May 23 '17 at 10:28
  • \$\begingroup\$ This should be updated for await / async - especially as Chrome console has top-level await. The "get the answer" part of the [code-golf] snippet can be shortened to (await f()).reputation. \$\endgroup\$ – Riking Jan 11 '18 at 8:11
12
\$\begingroup\$

black-box functions to be taken as input can be assumed to be predefined under a given name

This holds for languages that do have classical functions, for instance: Java, JavaScript, VB, C, C++, TI-Basic (some versions), Python, Octave, Matlab, Haskell, Lisp

This proposition is the opposite of that one which only allows this method of taking input for languages that cannot take functions as arguments.

\$\endgroup\$
  • 2
    \$\begingroup\$ I'm not entirely sure what is meant by this, but if I interpret it correctly, this will have my downvote, as inputs are never assumed to be stored in a variable already, so why are functions any different? \$\endgroup\$ – Sanchises Aug 13 '17 at 22:23
  • 4
    \$\begingroup\$ @Sanchises The problem is a lack of other ways for some languages. E.g. before Java introduced lambdas recently (so everything before Java 8), there was no way for a function to take a "functional" input. \$\endgroup\$ – flawr Aug 13 '17 at 22:26
  • \$\begingroup\$ Maybe add this method as an exception to the other one, so only when your language does not have function pointers or similar? \$\endgroup\$ – Sanchises Aug 13 '17 at 22:31
  • \$\begingroup\$ @Sanchises I now added a second proposal that includes your suggestion, see linked. \$\endgroup\$ – flawr Aug 14 '17 at 21:04
11
\$\begingroup\$

Metaprogramming languages may return a boolean by compiling/not compiling

For example, C++ template metaprogramming has no "return value". A common way for a C++ template metaprogramming to reject something is to fail to compile (sometimes in a SFINAE way, sometimes not).

\$\endgroup\$
  • \$\begingroup\$ if C++ doesn't compile, it prints to STDERR? If so, this could be generalized to allow boolean output by STDOUT vs STDERR. \$\endgroup\$ – Nathan Merrill Nov 13 '18 at 2:28
  • \$\begingroup\$ @NathanMerrill If something does not compile, it doesn't output at all, I think. Expect compiling/not compiling (as described here). \$\endgroup\$ – val yesterday
11
\$\begingroup\$

Regexes may output via any captured groups

Users can specify which subset of captured group(s) contains the output; it doesn't have to be just group 0.

\$\endgroup\$
  • 1
    \$\begingroup\$ perhaps reword to "a specified subset of captured groups" any could be mis-interpreted as allowing a different subset at each call. \$\endgroup\$ – Jasen Dec 22 '16 at 10:51
  • 1
    \$\begingroup\$ Is this to be interpreted such that the specification of which backref(s) contain the output is not counted towards the byte length of the regex program? I would strongly disagree with that, since the backref numbers are an integral part of the program and without them, it would not do anything useful. Is there any other language with a rule like this? At best, any regex answer that uses this rule should be treated as a distinct language which varies depending on the exact combination of backref numbers, like command-line parameters. \$\endgroup\$ – Deadcode Feb 26 at 19:29
  • 1
    \$\begingroup\$ @Deadcode yes, there are other languages with rules like this: asm may output via registers, and specifying the register numbers doesn't add to your byte count. \$\endgroup\$ – Grimy Feb 28 at 9:03
  • 1
    \$\begingroup\$ Should non-participating capture groups count as empty string / 0 for the purpose of this rule? \$\endgroup\$ – Grimy Feb 28 at 9:23
  • \$\begingroup\$ Also, for unary regexes, can the meaning of the capture groups be specified to include combinations of groups to make output of larger numbers possible, for example \4*\2^2 + \5*\2 + \6 or examples such as those shown here? Without this, it is only possible to output numbers less than or equal to the input (or the largest input number, in the case of multiple delimited input numbers). \$\endgroup\$ – Deadcode Feb 28 at 22:41
10
\$\begingroup\$

Regexes may output a number via the number of matches

Or truthy / falsey as whether they match.

\$\endgroup\$
  • 11
    \$\begingroup\$ Plain regex submissions are still not generally accepted as valid so I'm not sure it's worth adding defaults for this very specific case that a lot of people don't accept at all. \$\endgroup\$ – Martin Ender May 30 '15 at 10:27
  • 2
    \$\begingroup\$ Again , Retina. \$\endgroup\$ – CalculatorFeline Apr 2 '16 at 16:14
10
\$\begingroup\$

Programs may input/output bytes in a bit-serial or 8-bit parallel manner with a clock and enable signal

This is mainly applicable to HDLs such as Verilog, as well as other embedded devices (such as microcontrollers). Currently, $display is capable of writing to standard out in a simulator, but of course, not in a physical FPGA.

\$\endgroup\$
  • \$\begingroup\$ Why require an enable signal? If you take a clock as input, and can output a bit with every clock pulse, the enable signal is just wasting code. (I've used this in a submission already, although the question specified I/O methods explicitly rather than defaulting to this post.) \$\endgroup\$ – user62131 Jan 26 '17 at 20:40
  • \$\begingroup\$ @ais523 Sorry, I didn't think of that when I created this. It makes sense that the clock could be stopped, at least for input, but not sure how a program could communicate that it wants to delay output for a few clock cycles while calculating. \$\endgroup\$ – hexafraction Jan 26 '17 at 20:46
  • 1
    \$\begingroup\$ If the problem's simple enough, the program can guarantee that it'll never have to delay output. (Note that you could define a fairly slow clock speed.) Of course, for some algorithms (especially ones that are slower than constant or linear-bounded time) the enable signal might be required. \$\endgroup\$ – user62131 Jan 26 '17 at 20:49
  • \$\begingroup\$ Why need a clock signal? It is also possible to just use UART with a given Clock Frequency. And why 8 Bit? If you want what stop you from using 4, 12, 16 or 27 Bits? \$\endgroup\$ – 12431234123412341234123 Jun 21 '17 at 11:12
  • \$\begingroup\$ @12431234123412341234123 There is often no good way to perform sequential or stateful computation in synthesizable Verilog without a clock signal. 8-bit is in alignment with the fact that 8-bit ASCII is common; you can submit a new answer that allows non-8-bit parallel inputs. Additionally, to output/input data a communication clock is required, whether for each word/bit or to actually run a UART. I would strongly doubt that anyone would want to spend valuable bytes on writing a UART, except perhaps in code-bowling challenges. \$\endgroup\$ – hexafraction Jun 21 '17 at 21:32
10
\$\begingroup\$

For Languages Not Containing A Formal Implementation Of An Input Or Output Data Type, The Data Type May Be Implemented As Standardized For The Language

For instance, in TIS-100, all inputs and outputs are integers ranging from -999 to 999. Therefore, arrays of positive integers are often terminated with zeroes. If a challenge asks to take in the array [3, 4, 5, 6] and return [6, 8, 10, 12], in TIS-100 the implementation would be to take in the sequence 3 4 5 6 0 and output the sequence 6 8 10 12 0.

\$\endgroup\$
9
\$\begingroup\$

Programs may write output to the clipboard

Complementing the programs may take input from the clipboard proposal.

\$\endgroup\$
  • 1
    \$\begingroup\$ I agree with this, this is just like the proposal where stack-based languages could write to the stack instead of outputting the result \$\endgroup\$ – Cows quack Feb 28 '17 at 10:05
  • 4
    \$\begingroup\$ This would mean that any program can compute the identity by doing nothing, right? That's kind-of weird. \$\endgroup\$ – xnor Feb 28 '17 at 21:17
  • \$\begingroup\$ While I agree that that is weird, I don't think that that is grounds to down vote. That is abusable in exactly 1 challenge, (and that loophole could be closed in that 1 challenge) \$\endgroup\$ – Nathan Merrill Mar 6 '17 at 14:14
  • 2
    \$\begingroup\$ If you favor this proposal, you should also allow the identity to be computed by outputting on stdin. \$\endgroup\$ – feersum Mar 30 '17 at 2:27
9
\$\begingroup\$

When a challenge asks for an array as input/output, you may use a list instead, and vice-versa

Note that some languages may lack one of these concepts. For example, TI-Basic does not have an array datatype (but has lists) and C does not have a list in its standard library, but has arrays as part of the language.

In keeping with the general flexibility of I/O formats, I suggest we allow to use them interchangeably for all languages.

\$\endgroup\$
  • 4
    \$\begingroup\$ A fairly recent meta discussion which basically reached the conclusion that saying "list" in a challenge description is equivalent to any number of possible implementations in different languages which may or may not have different names. To put it a different way, I agree with your intention but I don't think it needs to be specified that "different languages use different words for fundamentally equivalent concepts". \$\endgroup\$ – Kamil Drakari Jul 18 '18 at 16:14
9
\$\begingroup\$

Lists of decimal digits and strings that match ^[0-9]*$ are interchangeable...

...but not when this may make the input format inconsistent.

For example, let's say you can assume the string input will match ^[1457]+$, which is a subset of ^[0-9]*$. Then, '1775' can also be input as [1, 7, 7, 5].

However, for a counterexample, let's assume the string input will match ^[a0-8]{2,15}$, which isn't a subset of ^[0-9]*$. In this case, no, you may not use a list of digits, since you can't use one to represent 1aa7708, for example. You can use [1, 1, 8, 8, 6, 4] to represent 118864, but this inconsistency is invalid by default.


I posted this after having come across numerous occurrences where I've had to ask about this in the comment section of the question, so I thought "why not post this here?". Surprisingly, this, AFAICT, hasn't ever been posted yet! I definitely have a positive opinion.

\$\endgroup\$
9
\$\begingroup\$

Programs running on FPGAs may take input or give output through transceivers.

\$\endgroup\$
  • \$\begingroup\$ I'm not familiar with FPGAs, but I would upvote if there's no way to encode data into which transceiver is being used or how. \$\endgroup\$ – lirtosiast Dec 3 '18 at 23:28
8
\$\begingroup\$

Programs may output using their return value

Iff it is legit regarding their specs and compilers.

Certain language treat programs as function, allowing programs to communicate via returns. Hence, we shoule allow such programs to be considered as whole, and not as snippet.

\$\endgroup\$
  • \$\begingroup\$ This has my upvote, because it is natural to languages as lua, where we often use this mechanism when creating an object. \$\endgroup\$ – Katenkyo Aug 9 '16 at 21:46
  • 4
    \$\begingroup\$ Is this different from meta.codegolf.stackexchange.com/a/5330/8478? \$\endgroup\$ – Martin Ender Aug 9 '16 at 22:30
  • \$\begingroup\$ @MartinEnder: This refers to codegolf.stackexchange.com/a/88856/15 this answer here and explicitly concerns itself with return values of programs that are not integers. Which, incidentally, doesn't work in any environment I know of. \$\endgroup\$ – Joey Aug 10 '16 at 7:00
  • \$\begingroup\$ Ok, basically you are referring to the fact that you can embed Lua and have the host application handle any return values from the "main" Lua program in a suitable manner. It's just that the default Lua interpreter will effectively do nothing with the stack contents in that case, right? \$\endgroup\$ – Joey Aug 10 '16 at 17:57
  • \$\begingroup\$ @MartinEnder I think it is a generalisation of the one you're pointing to. I'm only creating this post as some PPCGer think it doesn't apply to returns, and I'd like to have a clear consensus on the return for programs, instead of one that simply embed it because of its definition. \$\endgroup\$ – Katenkyo Aug 11 '16 at 7:35
  • \$\begingroup\$ @joey in lua, you can see this value the same way you would do for a library. As this is generally some short piece of code, you could use both local answer = require"codegolf" and local answer = loadstring("program copypasted")() \$\endgroup\$ – Katenkyo Aug 11 '16 at 7:39
7
\$\begingroup\$

Assembly language / machine code must take input and output as defined by an ABI for the platform that predates the challenge

For example, most commonly used ABIs for x86 state that 32-bit integer outputs are returned in the %eax register. As such, that's a valid way to output a 32-bit integer. However, outputting in, e.g., %esp would not be valid, unless a pre-existing ABI can be located where that's considered a valid method of output.

(Some background for people unaware of assembly language technology: an ABI, or Application Binary Interface, is a specification for how pieces of code within a particular program, on a particular platform, communicate with each other via the state of the processor. Most ABIs specify specific memory addresses and/or registers to be used for the communication.)

This is outright intended as a competing answer to the other assembly-language-related answers, because I feel they're too permissive and could lead to loopholes in which bytes are moved from the program to a selection of a register/memory address. (See this answer and the corresponding question for more detail.)

\$\endgroup\$
  • \$\begingroup\$ Assembly is free for platform etc it can have arg as one register and return its result as a flag... If it has to communicate or to be used to C language programs than yes has to follow API... \$\endgroup\$ – RosLuP Feb 4 at 22:27
7
\$\begingroup\$

A multidimensional rectangular array may be represented by the list [dimensions, flattened array]

\$\endgroup\$
6
\$\begingroup\$

Functions may output by calling a callback function or event

The callback function should be provided using a valid input method.

Example in JavaScript that outputs Hello world!:

f=>f("Hello world!")

This may be shorter than returning a value in some languages:

import random as r
def f(x):
    q=r.random()
    return(q+q/2)/2

As opposed to:

import random as r
def f(x,g):
    q=r.random()
    g((q+q/2)/2)
\$\endgroup\$
  • \$\begingroup\$ I don't see why this should be useful, considering you can just return the result. \$\endgroup\$ – Esolanging Fruit Feb 11 '17 at 2:27
  • \$\begingroup\$ Never mind, I see why this might be useful. Upvoted. \$\endgroup\$ – Esolanging Fruit Feb 13 '17 at 0:16
  • \$\begingroup\$ @Challenger5 Please explain why. \$\endgroup\$ – haykam Feb 28 '17 at 12:34
  • 1
    \$\begingroup\$ @haykam In a language like Python, something like import os [\n] def f(k):return os.system(k) is longer than import os [\n] def f(k,g):g(os.system(k)) \$\endgroup\$ – Esolanging Fruit Feb 28 '17 at 17:51
  • 4
    \$\begingroup\$ @Challenger5, the point of specifying default I/O methods is to allow answers to use idiomatic I/O, not to allow golfing a byte off print. \$\endgroup\$ – Peter Taylor Mar 1 '17 at 15:04
  • 1
    \$\begingroup\$ @PeterTaylor Might it be more idiomatic in some languages to use continuation-passing style rather than returning? \$\endgroup\$ – Esolanging Fruit Apr 26 '17 at 16:49
6
\$\begingroup\$

BASIC and related languages may take input via DATA / READ

In various dialects of BASIC, you can include data in the source code by using the DATA statement:

DATA abc, xyz
DATA 2, 3, 5, 7, 11

(Examples are written in QBasic because that's what I know.)

You access this data by using READ:

READ str1$, str2$
FOR i = 1 TO 5
  READ primes(i)
NEXT

This input method is idiomatic for BASIC.

\$\endgroup\$
  • \$\begingroup\$ Is this basically the equivalent of final static in Java? \$\endgroup\$ – Nathan Merrill Mar 5 '17 at 5:03
  • \$\begingroup\$ @NathanMerrill I don't think it's similar at all, actually. Based on 2 mins of research on Java, it looks like final is equivalent to CONST in QBasic, and static doesn't have an equivalent because QBasic doesn't have classes. READ is like INPUT, except the input is stored in the program with DATA so the user doesn't have to retype it each time they run the code. \$\endgroup\$ – DLosc Mar 5 '17 at 5:42
  • \$\begingroup\$ I guess I don't follow: when would you use const vs data/read? It seems like reading data already stored in your program is a waste of CPU cycles. \$\endgroup\$ – Nathan Merrill Mar 5 '17 at 14:32
  • \$\begingroup\$ @NathanMerrill Initializing arrays is one use case, since as far as I know BASIC doesn't have anything like C's int array[3] = {2,3,5};. In QBasic you DIM array(3) and then you can either do array(1)=2: array(2)=3: array(3)=5 (etc.) or you can write an easy-to-read FOR loop as above and put DATA 2, 3, 5 in the program. For large amounts of data (like layouts for a maze game), it might be better programming practice to put the data into a different file, but in the interests of being self-contained and not having to deal with file I/O, the DATA statement is often used instead. \$\endgroup\$ – DLosc Mar 6 '17 at 7:09
  • \$\begingroup\$ Note also that READ does not result in fixed values like CONST does. If you do CONST pi = 3.14, you can never modify that value (assigning to pi thereafter is a syntax error). But you can READ x and then do x=x+1 (or whatever). And you can READ multiple different data values into the same variable (in a loop, say). \$\endgroup\$ – DLosc Mar 6 '17 at 7:16
  • \$\begingroup\$ The argument is that the Data command effectively creates unnamed constant variables (and read just reads them), so the fact that you can modify the read ones is irrelevant IMO. I'm on the fence here: the syntax is similar to input from the user, but it also feels like reading from a variable. \$\endgroup\$ – Nathan Merrill Mar 6 '17 at 14:08
  • \$\begingroup\$ The ZX80 and ZX80 do not have DATA/READ keywords in its default interpreter. One has to store data in variables, or in REM statements and - in this case - read each byte by using the PEEK keyword. \$\endgroup\$ – Shaun Bebbers Apr 4 '17 at 9:03
  • \$\begingroup\$ Would the very similar <DATA> be accepted in Perl? (docs: perldoc.perl.org/SelfLoader.html#The-DATA-token). \$\endgroup\$ – Grimy May 31 '17 at 15:21
6
\$\begingroup\$

An array may be represented in input as a begin pointer and an end pointer.

like what's done in C++ std
\$\endgroup\$
  • \$\begingroup\$ So you mean the iterator model that the C++ STL uses? I like this. \$\endgroup\$ – Mego Mar 7 '18 at 3:46
  • 5
    \$\begingroup\$ "like what's done in C++ std" - that needs to be fleshed out a bit more, maybe with an example, for those unfamiliar with C(++). \$\endgroup\$ – Shaggy Mar 8 '18 at 8:15
  • \$\begingroup\$ @Shaggy What other language have pointer but not enough knower know C/C++? \$\endgroup\$ – l4m2 Mar 8 '18 at 12:02
  • 2
    \$\begingroup\$ @l4m2 Well, C and C#. Both of them have pointers and both of them have lots of users that don't know how the C++ standard library works. \$\endgroup\$ – Pavel Mar 12 '18 at 15:17
5
\$\begingroup\$

Programs may take input from the clipboard

\$\endgroup\$
2
\$\begingroup\$

Classes, objects or other entities may have separate methods to read the input and return the result

This includes the constructor and destructor.

\$\endgroup\$
  • \$\begingroup\$ Can the destructor return the result? That seems to be along the same lines as outputting to /dev/null. \$\endgroup\$ – Esolanging Fruit May 23 '17 at 3:15
  • \$\begingroup\$ I don't believe a C++ destructor is allowed to return anything. \$\endgroup\$ – Riking Jan 11 '18 at 8:21
  • \$\begingroup\$ Is it okay if the destruct function output something? \$\endgroup\$ – l4m2 Mar 8 '18 at 12:09
2
\$\begingroup\$

Functions may output via throwing the output value as an exception, if it could be caught, and read by the code catching the exception

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .