128
\$\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.


Return to FAQ index

\$\endgroup\$
2
  • \$\begingroup\$ Related (Can numeric input/output be in the form of byte values) \$\endgroup\$
    – mbomb007
    Commented Dec 13, 2016 at 16:10
  • 6
    \$\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
    Commented Jul 18, 2018 at 18:17

118 Answers 118

29
\$\begingroup\$

Assembly programs may read input from some specified memory location

If there are no I/O devices available, an answer might consist of a subroutine that reads its input values from some specified memory location (e.g. read a zero-terminated string starting at $0000).

\$\endgroup\$
3
  • 3
    \$\begingroup\$ This would be natural for various 8-bit micros. \$\endgroup\$
    – zwol
    Commented Mar 1, 2016 at 4:50
  • 1
    \$\begingroup\$ and for ms-dos where the command-line is at 80h \$\endgroup\$
    – Jasen
    Commented Dec 22, 2016 at 10:35
  • 1
    \$\begingroup\$ Seems pretty cheesy for normal x86/x86-64 code. I could use a register as an index into one input, and as a pointer to another input array that started at address 0. (xor edx,edx, mov eax, [rsi+rdx], add eax, [rdx]). I think it's a lot more reasonable to assume pointers to input/output arrays in registers of your choice (even if the standard calling convention uses stack args), except for things like 8-bit micros. \$\endgroup\$ Commented Jul 17, 2017 at 1:51
28
\$\begingroup\$

Multiple arguments to a function can be taken as a list or tuple instead

For example, if you needed to take in two integers as arguments, you can take one argument: a list of both integers.

Apparently, this isn't already here, so I'm posting this answer.

\$\endgroup\$
3
  • \$\begingroup\$ I totally agree. \$\endgroup\$ Commented Jul 31, 2023 at 13:47
  • \$\begingroup\$ What about the opposite? When the challenge has a fixed-length array as input, can we use a function with 5 arguments? \$\endgroup\$
    – corvus_192
    Commented Nov 5, 2023 at 23:07
  • \$\begingroup\$ I think that should be allowed as well…I mean, it makes sense. \$\endgroup\$
    – Someone
    Commented Nov 22, 2023 at 18:42
27
\$\begingroup\$

Full programs may take array/string input (of unknown length) via stdin in the form of <length> <contents> or <contents> <sentinel>

EOF handling is a common problem in many languages, not just esolangs. But only "regular" languages that support proper arrays and functions can circumvent this issue by writing functions instead of full programs.

Most programming contest problems (which only support stdin/stdout as I/O) solve this issue by giving extra information in the standard input in one of the two ways:

  • give "how many items you need to expect in the input" in the standard input itself, before the start of the actual content, or
  • give a special value at the end of input so that you can "terminate the program when this value is entered".

I believe these solutions are also a natural extension of

to full programs respectively, which currently only apply to functions.

The same condition for the sentinel value applies when the sentinel value method is used:

It should be noted that the sentinel value must be the one that is guaranteed to not appear in any valid input [...], since their presence breaks the premise of the I/O method.

\$\endgroup\$
26
\$\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\$
26
\$\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\$
1
  • 2
    \$\begingroup\$ This is now allowed by the sequence tag default. \$\endgroup\$
    – lirtosiast
    Commented Jul 18, 2019 at 20:44
25
\$\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\$
10
  • \$\begingroup\$ Do you think it should be restricted to C? \$\endgroup\$
    – Keyu Gan
    Commented Jul 12, 2017 at 3:46
  • 3
    \$\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
    Commented Jul 12, 2017 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
    Commented Jul 14, 2017 at 9:12
  • 10
    \$\begingroup\$ I'd say this should only be for a language where this is the natural representation of an array \$\endgroup\$
    – Justin
    Commented Jul 19, 2017 at 20:15
  • 1
    \$\begingroup\$ @keyugan C# supports both standard arrays and C-style pointers \$\endgroup\$
    – Pavel
    Commented Nov 8, 2017 at 23:51
  • 1
    \$\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
    Commented Nov 9, 2017 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
    Commented Nov 9, 2017 at 15:06
  • 2
    \$\begingroup\$ @KeyuGan Please don't make drastic changes to suggestions. \$\endgroup\$
    – user45941
    Commented Nov 12, 2017 at 4:56
  • \$\begingroup\$ How does it affect golfing? It is pretty much the standard way to handle arrays in C. Is the pointer-length pair in the arguments not included in the program length? \$\endgroup\$
    – Alex
    Commented Jan 20, 2021 at 14:23
  • 1
    \$\begingroup\$ @Alex This establishes that it is an allowable way of taking input in C, or any other pointer based language. Otherwise, it would be nearly impossible for C to take a list as input. \$\endgroup\$
    – MegaTom
    Commented Jan 22, 2021 at 1:11
24
\$\begingroup\$

Assembly programs may write output to registers

If there are no I/O devices available, an answer might consist of a subroutine that leaves its computed output values in the machine registers upon returning.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Most calling conventions return integer values in a register, so this is eminently sensible for code fragments and functions. \$\endgroup\$ Commented Jan 1, 2017 at 7:12
  • \$\begingroup\$ This needs clarification: can we use any register, or only the ones that would be specified to be used by the ABI? \$\endgroup\$
    – user62131
    Commented Jun 11, 2017 at 4:14
  • \$\begingroup\$ This answer is A, yours is B. \$\endgroup\$ Commented Jun 20, 2017 at 2:15
  • 2
    \$\begingroup\$ @user62131: in real-life hand-written assembly language, you do sometimes write helper functions with a totally custom calling convention that return multiple values in registers that are convenient. i.e. in asm and machine-code answers, you can invent any reasonable calling convention for your function. See my calling-convention answer on Tips for golfing in x86/x64 machine code \$\endgroup\$ Commented Oct 8, 2019 at 18:06
22
\$\begingroup\$

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

\$\endgroup\$
6
  • 2
    \$\begingroup\$ We now have Retina for regex so this can \$\endgroup\$ Commented Apr 2, 2016 at 16:13
  • \$\begingroup\$ @CalculatorFeline Retina is actually Turing Complete. \$\endgroup\$
    – mbomb007
    Commented Feb 17, 2017 at 19:14
  • \$\begingroup\$ ...I'm confused. \$\endgroup\$ Commented Feb 20, 2017 at 23:34
  • 3
    \$\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
    Commented Apr 27, 2017 at 15:56
  • \$\begingroup\$ True. But Retina is basically regex+. Do we need this anymore? \$\endgroup\$ Commented Apr 28, 2017 at 21:59
  • \$\begingroup\$ I don't think using plain regex is even allowed, since it isn't Turing complete. \$\endgroup\$
    – 12Me21
    Commented Mar 2, 2018 at 2:22
21
\$\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\$
16
  • \$\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\$ Commented Nov 27, 2015 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\$ Commented Nov 27, 2015 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 Mod
    Commented Nov 27, 2015 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\$ Commented Nov 27, 2015 at 18:25
  • \$\begingroup\$ Yes, it does, assuming a GET request becomes an accepted input input. \$\endgroup\$
    – Dennis Mod
    Commented Nov 27, 2015 at 18:37
  • \$\begingroup\$ Then there is the program <?php eval(key($_GET));. (SQL table input may have similar problems.) \$\endgroup\$
    – jimmy23013
    Commented Nov 27, 2015 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\$ Commented Nov 27, 2015 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\$ Commented Nov 27, 2015 at 20:32
  • \$\begingroup\$ @IsmaelMiguel As written, no. Those are neither GET nor POST parameters. \$\endgroup\$
    – Dennis Mod
    Commented Nov 27, 2015 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\$ Commented Nov 27, 2015 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\$
    – user45941
    Commented Nov 28, 2015 at 6:11
  • \$\begingroup\$ @Mego The -r is free. Read: meta.codegolf.stackexchange.com/q/2424/14732 \$\endgroup\$ Commented Nov 28, 2015 at 22:44
  • \$\begingroup\$ @IsmaelMiguel Fair enough. The rest of my comment still stands. \$\endgroup\$
    – user45941
    Commented Nov 28, 2015 at 23:42
  • \$\begingroup\$ @Mego It's still under discussion. \$\endgroup\$ Commented Nov 29, 2015 at 1:09
  • \$\begingroup\$ @IsmaelMiguel Hence why I offered my opinion \$\endgroup\$
    – user45941
    Commented Nov 29, 2015 at 1:58
21
\$\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\$
2
  • \$\begingroup\$ Huh? I'm confused what makes this different from passing inputs as arguments in general. \$\endgroup\$ Commented Sep 30, 2018 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
    Commented Sep 30, 2018 at 12:07
21
\$\begingroup\$

You may assume that the input is valid

When parsing an input, you may assume that the input is already valid. You are not required to validate it unless the challenge is specifically about testing whether the input is valid or not.

\$\endgroup\$
20
\$\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\$
5
  • 3
    \$\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\$
    – user45941
    Commented Mar 20, 2017 at 4:12
  • \$\begingroup\$ @Mego Thanks, I agree. I thought that was implicit, but I've added a mention to the post. \$\endgroup\$ Commented Mar 20, 2017 at 16:36
  • \$\begingroup\$ How many brackets at the last is allowed? 1? or allowed if not that large(only avoid loophole) \$\endgroup\$
    – l4m2
    Commented Dec 20, 2017 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\$ Commented Dec 21, 2017 at 15:31
  • \$\begingroup\$ @l4m2, I thought about any constant (not related to input) number. \$\endgroup\$
    – Qwertiy
    Commented May 25, 2019 at 22:21
19
\$\begingroup\$

Programs may take input from a file

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

Programs and functions may return a boolean value via the presence or absence of output

\$\endgroup\$
2
  • 1
    \$\begingroup\$ This is for decision-problem formats, I presume. \$\endgroup\$
    – Giuseppe
    Commented Aug 12, 2019 at 19:26
  • \$\begingroup\$ @Giuseppe Yep. Modeled it after the similar one for errors. \$\endgroup\$ Commented Aug 12, 2019 at 19:28
17
\$\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\$
1
  • 10
    \$\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\$ Commented Jul 18, 2018 at 16:14
17
\$\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\$
16
\$\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\$
5
  • \$\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
    Commented May 12, 2017 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
    Commented May 12, 2017 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
    Commented May 17, 2017 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
    Commented May 23, 2017 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
    Commented Jan 11, 2018 at 8:11
16
\$\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\$
4
  • 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
    Commented Aug 13, 2017 at 22:23
  • 6
    \$\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
    Commented Aug 13, 2017 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
    Commented Aug 13, 2017 at 22:31
  • \$\begingroup\$ @Sanchises I now added a second proposal that includes your suggestion, see linked. \$\endgroup\$
    – flawr
    Commented Aug 14, 2017 at 21:04
16
\$\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\$
2
  • \$\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\$ Commented Nov 13, 2018 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\$ Commented Jul 17, 2019 at 13:57
16
\$\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\$
5
  • 2
    \$\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
    Commented Dec 22, 2016 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
    Commented Feb 26, 2019 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\$
    – Grimmy
    Commented Feb 28, 2019 at 9:03
  • 1
    \$\begingroup\$ Should non-participating capture groups count as empty string / 0 for the purpose of this rule? \$\endgroup\$
    – Grimmy
    Commented Feb 28, 2019 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
    Commented Feb 28, 2019 at 22:41
16
\$\begingroup\$

Strings, arrays of characters, and arrays of bytes are interchangeable

Surprised this isn't here yet.

Languages like C don't really have strings, and arrays of bytes (*char) is already the common way of representing them. C++ does have strings but most CGSE answers still seem to prefer pointers to arrays of bytes.

Other languages like Rust have strings that are really inconvenient to work with, so I've already been using [u8] and Vec<u8> most of the time.

Answers may in some cases prefer using string tools on arrays, as long as the array will never have values over 255 taking input as a string then using functions like .replace() and .split() would be valid.

Or some languages, like regex, have only strings and would make some number challenges very inconvenient without using char values.

Actually this is already the default as stated here but I think including it here is still valuable since this post is a lot easier to find.

\$\endgroup\$
15
\$\begingroup\$

Regexes may output a number via the number of matches

Or truthy / falsey as whether they match.

\$\endgroup\$
2
  • 12
    \$\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 Mod
    Commented May 30, 2015 at 10:27
  • 4
    \$\begingroup\$ Again , Retina. \$\endgroup\$ Commented Apr 2, 2016 at 16:14
15
\$\begingroup\$

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

like what's done in C++ std

\$\endgroup\$
4
  • 1
    \$\begingroup\$ So you mean the iterator model that the C++ STL uses? I like this. \$\endgroup\$
    – user45941
    Commented Mar 7, 2018 at 3:46
  • 7
    \$\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
    Commented Mar 8, 2018 at 8:15
  • \$\begingroup\$ @Shaggy What other language have pointer but not enough knower know C/C++? \$\endgroup\$
    – l4m2
    Commented Mar 8, 2018 at 12:02
  • 4
    \$\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
    Commented Mar 12, 2018 at 15:17
14
\$\begingroup\$

Programs may write output to the clipboard

Complementing the programs may take input from the clipboard proposal.

\$\endgroup\$
5
  • 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\$
    – user41805
    Commented Feb 28, 2017 at 10:05
  • 5
    \$\begingroup\$ This would mean that any program can compute the identity by doing nothing, right? That's kind-of weird. \$\endgroup\$
    – xnor
    Commented Feb 28, 2017 at 21:17
  • 3
    \$\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\$ Commented Mar 6, 2017 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
    Commented Mar 30, 2017 at 2:27
  • \$\begingroup\$ @xnor: You can already do that in most languages by taking input by reference and not modifying it. (In some languages like asm and classic C, you don't even have to declare args, so a function can just be empty or a single return instruction. That's UB in C but will happen to work; compilers only warn instead of erroring when a function definition doesn't match how its called, and for use in other compilation units you can provide a non-prototype declaration.) I guess in many other languages, it would save some space. But only for the identify function. \$\endgroup\$ Commented Apr 14, 2023 at 7:11
14
\$\begingroup\$

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

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

Floating point numbers may be represented as fractions.

For example, instead of literal 0.5 or "0.5", the program can return "1/2" or [1,2]. It's especially useful for languages without out-of-the-box floating point support.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ But does the fraction have to be reduced? \$\endgroup\$
    – Adám
    Commented Jun 10, 2020 at 15:16
  • 1
    \$\begingroup\$ I leave it up as a consensus. Reducing a fraction isn't a very complex task, but if it is necessary (due to some loophole I can't find currently) then reducing the fraction might be necessary. \$\endgroup\$ Commented Jun 10, 2020 at 16:56
14
\$\begingroup\$

Spreadsheet languages can take input from a specific cell / group of cells

Most Excel / Google Sheets answers seem to do this anyway, I don't know why it wasn't here.

For example, this answer takes input through the A1 cell of a spreadsheet and performs operations on that to arrive at the final output.

\$\endgroup\$
0
14
\$\begingroup\$

A function can output by modifying a global variable if that variable is automatically outputted/displayed

It has been decided that in general, functions may not output by just modifying a global variable because that would make it too much like a snippet, which is not allowed.

But what about languages where the value of that global variable is automatically outputted? For example, say there is a language where output is stored in an output string variable, and automatically displayed when the program finishes execution.

For example, in Scratch, the current ruling is that output must be displayed via the say block. However, in Scratch variables are shown automatically if not unchecked in the interface, just as say shows output automatically if the sprite is not checked as hidden in the interface. These should be allowed interchangeably since both output a value without any extra code at the call site.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Isn't this implied by output to the screen? \$\endgroup\$
    – Bbrk24
    Commented Aug 4, 2023 at 12:55
  • \$\begingroup\$ @Bbrk24 I suppose so, I mainly posted this because I don't think Scratch answers should have to say the output \$\endgroup\$ Commented Aug 4, 2023 at 13:00
  • \$\begingroup\$ But a global variable displayed is indeed outputted to the screen. \$\endgroup\$ Commented Aug 13 at 11:07
13
\$\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\$
6
  • \$\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
    Commented Aug 9, 2016 at 21:46
  • 4
    \$\begingroup\$ Is this different from meta.codegolf.stackexchange.com/a/5330/8478? \$\endgroup\$
    – Martin Ender Mod
    Commented Aug 9, 2016 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
    Commented Aug 10, 2016 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
    Commented Aug 10, 2016 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
    Commented Aug 11, 2016 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
    Commented Aug 11, 2016 at 7:39
13
\$\begingroup\$

Programs may take input as arguments to command line flags

If a language has a command line flag in the form --flag=arg, input may be taken as the argument to that flag. For example:

./language -f --option=2 <prog>

Can be run for the input 2.

\$\endgroup\$

You must log in to answer this question.

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