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

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

116 Answers 116

12
\$\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
    Aug 9, 2016 at 21:46
  • 4
    \$\begingroup\$ Is this different from meta.codegolf.stackexchange.com/a/5330/8478? \$\endgroup\$ 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
    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
    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
    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
    Aug 11, 2016 at 7:39
12
\$\begingroup\$

Numerical input and output can be given as a character code

For example, the input 64 can instead be given as the character @. This is mainly used for more esoteric languages that can only take input through character codes, for example brainfuck.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Note that this was already a consensus here, I'm just adding it here to make it easier to find. \$\endgroup\$
    – Jo King Mod
    Aug 28, 2019 at 12:47
12
\$\begingroup\$

An array may be represented as a buffer terminated with a sentinel value

This can be applied to both inputs and outputs for languages such as C and assembly. This is the default for C strings (a buffer of char, terminated with 0), but can be applied to any other data type.

As a more concrete example:

  • An input array of positive integers can be taken as a single pointer of uint32_t or any other convenient integer type, which is assumed to terminate with a single zero.
  • For an output array of positive integers, a C function can take an output pointer as the start of the buffer, write several numbers on that buffer, and then write a zero at the end.

It should be noted that the sentinel value must be the one that is guaranteed to not appear in any valid input or output, since their presence breaks the premise of the I/O method. For example, if the input may contain zeros, you cannot use zero as the sentinel value for the input array.

\$\endgroup\$
12
\$\begingroup\$

A program or function taking input by reference may output by modifying the input value in place

A complement to this

For example, in rust, one might write

fn a(v:&mut Vec<i32>){v.push(7);}

instead of

fn b(mut v: Vec<i32>)->Vec<i32>{v.push(7);return v;}

or

fn c(v:&mut Vec<i32>)->&mut Vec<i32>{v.push(7);return v;} 
\$\endgroup\$
11
\$\begingroup\$

Programs may take input from the clipboard

\$\endgroup\$
1
  • \$\begingroup\$ why would anyone downvote this? i see literally no reason this shouldnt be allowed, its just another place to grab input from :P \$\endgroup\$ Jun 23, 2022 at 19:47
11
\$\begingroup\$

SQL-based languages can output via SELECT or PRINT

We have countless examples of highly upvoted answers sending SQL output to either the results pane (via SELECT) or to the messages pane (via PRINT), depending on which works the best and is shorter for a particular challenge.

Both are acceptable.

(This uses terminology from MS SQL Management Studio, other platforms/clients likely have equivalents.)

Other outputs may be possible as well, such as outputting graphical results to the spatial results pane.

\$\endgroup\$
10
\$\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\$
11
  • \$\begingroup\$ Is this basically the equivalent of final static in Java? \$\endgroup\$ Mar 5, 2017 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, 2017 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\$ Mar 5, 2017 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, 2017 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, 2017 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\$ Mar 6, 2017 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\$ Apr 4, 2017 at 9:03
  • \$\begingroup\$ Would the very similar <DATA> be accepted in Perl? (docs: perldoc.perl.org/SelfLoader.html#The-DATA-token). \$\endgroup\$
    – Grimmy
    May 31, 2017 at 15:21
  • \$\begingroup\$ @DLosc - is there a consensus as to what portions of the Read and Data statements are to be included in the bytecount? Is this Yabasic snippet scored correctly? \$\endgroup\$ Apr 8, 2022 at 4:58
  • 1
    \$\begingroup\$ @TaylorRaine That's a good question. I don't think there is a consensus. My initial reaction is that DATA should be included in the bytecount--only the data itself should not be. But that still raises questions about the comma in DATA 2,3... \$\endgroup\$
    – DLosc
    Apr 8, 2022 at 15:43
  • \$\begingroup\$ @DLosc Meta Question about the above \$\endgroup\$ Apr 8, 2022 at 16:58
10
\$\begingroup\$

Numerical input and output can be given as a string of digits and vice-versa

As long as the input/output format is consistent, you may take a numerical output such as f(123) as a string f("123") and vice-versa. This will also open up other string methods, such as taking a number as a list of digits as per this consensus.

Note that if different behaviours are expected for numbers with leading zeroes, then you will have to take input as a string instead, since strings with leading zeroes will be mapped to the same number.

You may not add leading zeroes if you are using string input instead of a number.

\$\endgroup\$
0
10
\$\begingroup\$

Image output may consist of a matrix or 2d array for raster images

This is relevant e.g. for challenges. (e.g. for languages that otherwise have no way of writing an image file or opening a window for the graphical display)

\$\endgroup\$
10
\$\begingroup\$

Spreadsheet languages may output through cell values

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

For example, this answer outputs a created piece of text to the cell it's pasted into.

\$\endgroup\$
9
\$\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\$
5
  • \$\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, 2017 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\$
    – nanofarad
    Jan 26, 2017 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, 2017 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\$ Jun 21, 2017 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\$
    – nanofarad
    Jun 21, 2017 at 21:32
9
\$\begingroup\$

Mappings may be used in place of functions

(Discussion in The Nineteenth byte)

Whenever a function is expected, a mapping such as a dictionary/hashmap or array may be used instead.

Arguments for:

  • in many languages, there is no syntax difference (K, MATLAB, Ruby - a Proc can be used with the same [] syntax as a Hash)
  • disallowing it borders on an unobservable requirement, particularly in the above languages, and having different rules for different languages is generally discouraged
  • it allows removing boilerplate in challenges which boil down to "construct a mapping and then look up into it", like (Python) lambda x:{k:v for k,v in ...}[x]

Arguments against:

  • it feels a bit cheaty to me
\$\endgroup\$
4
  • \$\begingroup\$ I haven't downvoted, because I know mathematically a function can be viewed as just a (possibly infinite) mapping. But I also haven't upvoted because, as you say, it feels a little cheaty. I think if the return value isn't callable/executable in your chosen language, we shouldn't assume an interpreter to run it. \$\endgroup\$ May 30, 2021 at 1:23
  • 3
    \$\begingroup\$ I have no reasons to disagree for challenges where the output is completely deterministic (i.e. a pure function), assuming that you can extract the output for any given input in finite time. The answer is invalid if the premise is not satisfied (which is equivalent to a function that falls into an infinite loop for that input). One thing to note is that we're already allowing this for sequence challenges in the form of infinite lazy lists. \$\endgroup\$
    – Bubbler
    Jun 1, 2021 at 7:58
  • 3
    \$\begingroup\$ Even if this is to be allowed as an output method, it should certainly not be allowed as an input method (for problems where you take a function as input), since a mapping includes extra information that a function does not, namely an enumeration of all items in the domain. \$\endgroup\$ Jun 9, 2021 at 2:54
  • 1
    \$\begingroup\$ In languages without built-in mapping type, mappings are usually represented as lists of tuples. But using lists of tuples in place of functions feels even more cheaty. \$\endgroup\$
    – alephalpha
    Jan 29, 2023 at 2:41
9
\$\begingroup\$

When printing to STDOUT, you may have any number of trailing whitespace

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

Programs may take input from a generator or iterator

We already have a rule that programs may output via a generator. Just for completeness, I think we should specify it's a valid input method as well.

Example in rust:

|t:Box<dyn Iterator>|t.flatten()

Other example, taking input via the returned generator in python:

def k():
    yield "Hello, "+str(yield "What is your name")

Which can be used like

t = k()
print(next(k))
print(k.send("Charlie"))

Another example:

def k(f):
    return [int(next(f)),*f]
\$\endgroup\$
1
  • 3
    \$\begingroup\$ This would also work as (e.g. in Python) a function that takes a generator as an argument, and to take input, use next(g) (instead of yield). \$\endgroup\$
    – naffetS
    Aug 27, 2022 at 19:18
9
\$\begingroup\$

Infinite lists / generators / iterators may be taken as indexing functions

An infinite list / generator / iterator may be taken as a function that indexes into the value.

For example, if taking the infinite list of fibonacci numbers [1, 1, 2, 3, 5, 8] you may instead take a function that:

  • When given 0, returns 1
  • When given 1, returns 1
  • When given 2, returns 2
  • When given 3, returns 3
  • When given 4, returns 5

etc.

\$\endgroup\$
2
  • \$\begingroup\$ May finite lists be taken this way? \$\endgroup\$
    – noodle man
    Nov 3, 2023 at 17:58
  • \$\begingroup\$ @noodleman I think that falls under mappings being equivalent to functions (or if you don't think it does you might want to make a separate post), this is an edgecase mostly for languages that don't have built-in infinite lists. \$\endgroup\$
    – emanresu A
    Nov 3, 2023 at 19:32
8
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\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, 2018 at 23:28
  • \$\begingroup\$ Responding to the above comment which I missed five years ago - you pick a transceiver and its clock/config at design time. IMO using multiple transceivers would be in the same spirit as using multiple function arguments (in the sense of a arguments in a language like C) \$\endgroup\$
    – nanofarad
    Sep 19, 2023 at 16:40
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\$
2
  • \$\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\$
    – user58988
    Feb 4, 2019 at 22:27
  • \$\begingroup\$ In real-world hand-optimized assembly language, it's normal for some functions to use their own custom calling convention for input and output registers. If the language of your answer is machine code, it makes sense to think about a machine code caller. I agree that outputting via the stack pointer wouldn't be plausible, but any other register is fine, including a boolean in FLAGS. See Tips for golfing in x86/x64 machine code for x86(-64) specifically. \$\endgroup\$ Jul 21, 2023 at 11:49
7
\$\begingroup\$

If a program is supposed to take a whole number as input, it may accept the number as a unary string.

\$\endgroup\$
1
7
\$\begingroup\$

Functions may generate different output via different exceptions if they could be caught using different code

For example, in C++, you could use throw 1; and throw true; to output two distinct values. They could be caught by catch(int) and catch(bool) in a try block respectively. (In most other languages, you could only throw values in an exception class.)

\$\endgroup\$
7
\$\begingroup\$

Image output may consist of a pixel-shader

A pixel-shader is a function that takes the (x,y) coordinates of a pixel as an input, and outputs the color of this pixel (e.g. a scalar for grayscale images, a tuple for color images, a boolean value for binary images etc.)

This is relevant e.g. for challenges. (also for GLSL / Shadertoy answers.)

\$\endgroup\$
6
  • \$\begingroup\$ Isn't this explicitly disallowed for graphical-output at the moment? I suspect it had a good reason too. Same for your other suggestion here. \$\endgroup\$
    – Mast
    Nov 25, 2019 at 9:56
  • \$\begingroup\$ @Mast It seems to me that the tag wiki only addresses ASCII-art, but these two types do not seem to be captured, that is why I also thought it would be a good idea to clarify that explicitly. \$\endgroup\$
    – flawr
    Nov 25, 2019 at 10:15
  • 1
    \$\begingroup\$ I find it weird that this lets solutions not specify the size of the image. Or does the function gave to produce some different value indicating out-of-bounds? \$\endgroup\$
    – xnor
    Nov 25, 2019 at 10:25
  • \$\begingroup\$ @xnor The size of the image should be specified, but I think the key is that if you want a bigger resolution of the same image you could just sample (x,y) more finely as an alternative to defining a bigger resolution in the start. \$\endgroup\$
    – flawr
    Nov 25, 2019 at 10:59
  • \$\begingroup\$ @flawr I'm not clear how you'd sample more finely. Are (x,y) floats? \$\endgroup\$
    – xnor
    Nov 25, 2019 at 11:02
  • 1
    \$\begingroup\$ If we normalize the coordinates e.g. to [0,1]^2 then (x,y) could be interpreted as floats, right. If you're using integers then the program itself also takes the limits (e.g. in shadertoy this is implicit) \$\endgroup\$
    – flawr
    Nov 25, 2019 at 12:39
7
\$\begingroup\$

A natural number may be represented as a collection of n empty collections, and a string as a collection of such numbers

In languages that do not have numbers or strings but do have lists, stacks, or other collections, a natural number (including 0) may be represented by a collection of empty collections, where the length of the outer collection represents the natural number. So [] would represent 0, [[], [], []] would represent 3, and so on. Strings may be represented as collections of these natural numbers, as Unicode codepoints are unsigned.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Even if the language does not have a notion of a number or string, the interpreter can be made to do the necessary I/O via such conversion. Flurry already does that (a number is not a built-in, but stack contents are translated from/to numbers when the program starts/ends). \$\endgroup\$
    – Bubbler
    Jun 2, 2021 at 3:22
  • \$\begingroup\$ @Bubbler That's totally reasonable, but one of the main ideas of the language I was thinking about is that the interpreter has no conception of anything that isn't a stack. \$\endgroup\$
    – user
    Jun 2, 2021 at 12:55
7
\$\begingroup\$

In stack-based languages, inputs may be pushed onto the stack at the start of the program, and the output may be the top element on the stack at the end of the program

This has already been proposed for functions, but it should also be allowed for full programs.

\$\endgroup\$
7
\$\begingroup\$

When returning a set, one can return a map, where values are ignored

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

A full program may be input or output any time a function could

If we can do this:

x=>y=>x+y

Then we can do this:

print("print(int(input())+"+input()+")")
\$\endgroup\$
2
  • 16
    \$\begingroup\$ That Python code is...a curried program. what. \$\endgroup\$ Jun 20, 2017 at 2:18
  • 2
    \$\begingroup\$ +1 from me. If functions can return other functions, full programs should be able to print other full programs \$\endgroup\$
    – MilkyWay90
    Jun 16, 2019 at 18:25
5
\$\begingroup\$

black-box functions to be taken as input can be assumed to be predefined under a given name only if they cannot be passed as an argument.

This holds for languages that do have classical functions, but cannot pass around functions like other values. This holds for instance for: Java up to version 7

This proposition is the opposite of that one which does not have this restriction.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ -1 because I don't think this is well-defined (e.g. are functions in Underload passed as an argument? What about Python's functions, which aren't black box because you can inspect their bytecode?) \$\endgroup\$ Nov 26, 2017 at 23:39
  • 1
    \$\begingroup\$ Also, Don't allow / ask for different things in languages with different capabilities (related issue). \$\endgroup\$ Nov 26, 2017 at 23:45
  • 2
    \$\begingroup\$ @EsolangingFruit That doesn't seem to be the case here, it's not the challenge asking for different stuff for different languages, it's more like allowing more languages to compete by using more lenient consensuses for them, since otherwise they couldn't compete at all. \$\endgroup\$ Dec 10, 2017 at 12:14
  • 4
    \$\begingroup\$ @EriktheOutgolfer Fair, but my view is that, to avoid ambiguity and rule-lawyering, we should allow all languages to do this. \$\endgroup\$ Dec 10, 2017 at 20:57
5
\$\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\$
4
\$\begingroup\$

Functions are allowed to overwrite global data structures as its output

The premise of this is that your language does not support built-in return values.

For example, Assembly language doesn't have a built-in way of returning values, therefore a global accumulator is permitted as a return value.

In Scratch, there are no functions (only procedures), therefore global variables are allowed as the output of the function.

In stack-based languages, functions take arbitary items from the stack, and modify the stack. The return value is therefore stored onto the stack.

\$\endgroup\$
3
  • \$\begingroup\$ In Scratch, variables can be outputted via say. And in a most stack-based languages, there are commands to output. So only if your language has NO output method. \$\endgroup\$
    – emanresu A
    Mar 30, 2021 at 19:54
  • \$\begingroup\$ @emanresuA In Scratch, variables are shown automatically if not unchecked in the interface, in a similar way to how say shows output automatically if the sprite is not checked as hidden in the interface. Do you think in this case it is OK to not use say, or would you still say that say is better? \$\endgroup\$
    – noodle man
    Jan 25, 2023 at 14:54
  • 2
    \$\begingroup\$ @Jacob That's a good question, and I'd suggest making a separate post for that. \$\endgroup\$
    – emanresu A
    Jan 25, 2023 at 20:04
4
\$\begingroup\$

Output may have any number of leading white space.

There is already a suggestion concerning trailing blanks. This is about preceding blanks. Why should this be acceptable? Suppose you wanna to write a program in Pascal that prints the answer to the ultimate question of life, the universe, and everything. You’ll simply write

program p(output);begin writeLn(42)end.

However, when the destination is a text file (like output is), for numeric write arguments there’s an invisible implementation-defined default minimum-width specifier. Thus this program may print:

         42

i. e. the program is effectively doing a

writeLn(output, 42:11)

The :11 indicates the minimum number of characters the argument occupies (for numeric arguments, for char, string and Boolean it specifies the exact width). The default value is implementation-defined thus different compilers may have different default values, possibly even 1. If leading white space is prohibited you’ll have to write :1 yourself.

Ultimately, like trailing blanks it does not inhibit legibility. Specifically for many numbers it actually improves legibility, that’s why Pascal allows and some compilers do have a reasonably large default minimum-width.

\$\endgroup\$
4
\$\begingroup\$

Programs may take input based on their executable name.

i. e. argv[0] in C or $0 in shell-like languages. Thus the script parrot.sh

#!/bin/sh
echo $0

if having multiple links to it

ln parrot.sh foo
ln parrot.sh bar

prints different outputs

PATH=$(pwd) foo     #  will print e.g. /tmp/foo
PATH=$(pwd) bar     #  will print e.g. /tmp/bar

NB: This rule is not applicable if the executable file name has to be a certain string to fulfill the programming challenge’s specifications at hand.

\$\endgroup\$
8
  • \$\begingroup\$ This is a default loophole. If taking input through the executable name, those bytes count towards the score. \$\endgroup\$
    – Dingus
    Jan 6, 2021 at 1:57
  • 5
    \$\begingroup\$ @Dingus no, as in the user must rename the executable to a string, and that string is one of the inputs. It is not stored program data \$\endgroup\$
    – pxeger
    Jan 6, 2021 at 7:51
  • \$\begingroup\$ I don't really see how this isn't stored program data (where the data is one of the inputs). Leaving that aside, how is this different from hardcoding? \$\endgroup\$
    – Dingus
    Jan 6, 2021 at 10:50
  • \$\begingroup\$ In some sense I guess this isn't much different from taking input from a file, which is allowed. But I imagine that file is meant to be a fixed target, not a moving one. \$\endgroup\$
    – Dingus
    Jan 6, 2021 at 11:02
  • 4
    \$\begingroup\$ I don't really see how this is stored data. Instead of running ./program arg1 arg2, you run mv program arg0; ./arg0 arg1 arg2. \$\endgroup\$
    – pxeger
    Jan 6, 2021 at 11:10
  • 3
    \$\begingroup\$ Or you can run it without mv via something like exec -a arg0 ./program arg1 arg2 in bash or execlp("program", arg0, arg1, arg2, 0) in C. From an OS perspective, $0 is just a parameter that we usually leave set to the default value. \$\endgroup\$ May 30, 2021 at 1:26
  • 3
    \$\begingroup\$ I'll say, I don't know of a situation where this could save bytes, but I don't see a reason to ban it. It's not the same as the loophole of assuming the program will have a fixed value controlled by the programmer. \$\endgroup\$ May 30, 2021 at 1:28
  • \$\begingroup\$ Can someone give an example of a challenge that gets more interesting this way? \$\endgroup\$
    – Philippos
    Dec 8, 2023 at 7:59
4
\$\begingroup\$

When taking a set, one can take a map from the set to an well agreed value

This allows some languages to easier check if element in a set with mapped value true: X[i] is shorter than X.contains(i)

\$\endgroup\$

You must log in to answer this question.

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