28
\$\begingroup\$

Inspired by this

Task

Your task is to output the average character of your code.

What is an average character?

Say we have a string golf(). We then take the ASCII value of each separate symbol, so our string is 103 111 108 102 40 41, and calculate the average of those values, rounded to an integer. In our case the average is 505 / 6 = 84.1666... = rounded to 84. That value is printed out as an ASCII character, in our case T.

Rules

You must take no input and must not read the source code directly, while outputting the average character of its source code. Integers are rounded by function floor(x+0.5). Empty programs are not allowed. This is code-golf, so lowest byte count wins!

\$\endgroup\$
12
  • 4
    \$\begingroup\$ Average Character Calculator (only works for unicode characters input) \$\endgroup\$ Commented Jul 19, 2020 at 19:02
  • 7
    \$\begingroup\$ While I like the idea, it would have been beneficial to restrict code to contain at least two different characters, neither of which can be identical to the average. \$\endgroup\$
    – Adám
    Commented Jul 19, 2020 at 19:07
  • 2
    \$\begingroup\$ @Adám yeah, for some reason I didn't think of hardcoding the output... Maybe somebody could turn this into an interesting question, as I think its too late to edit this question. \$\endgroup\$
    – Dion
    Commented Jul 19, 2020 at 19:10
  • 2
    \$\begingroup\$ @Dion You could post in the Sandbox a variant where the scoring works best for longer programs, or where the average/sum plays some role (just spitballing ideas) \$\endgroup\$ Commented Jul 19, 2020 at 19:38
  • 3
    \$\begingroup\$ @Dion Empty program would result the average value of round(0 / 0). It is invalid simply due to divided by zero. So I don't think the empty source code should be allowed. If it must be allowed, by using the definition of division, x / y = z is y * z = x, one may argue output any single character should be allowed. \$\endgroup\$
    – tsh
    Commented Jul 20, 2020 at 6:25

76 Answers 76

2
\$\begingroup\$

Batch, 7 bytes

@echo U

I used the average character calculator to calculate the average of @echo (with trailing space) and just appended the result, as that won't change the average.

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

ArnoldC, 59 bytes

IT'S SHOWTIME
TALK TO THE HAND "A"
YOU HAVE BEEN TERMINATED

Try it online!

Test the average

Mandatory answer in ArnoldC. New line characters have been added to the average as a single character of value 10.

\$\endgroup\$
3
  • \$\begingroup\$ Sorry, I misunderstood what you meant by 'New line characters have been added to the average as a single character'. But with "B" and two newlines the average I get is 65.4237288, which rounds down to 65 (A). \$\endgroup\$
    – Dingus
    Commented Jul 22, 2020 at 11:42
  • \$\begingroup\$ @Dingus you're totally right, I don't know Python and I messed up the code of the average calculator trying to adapt it. ^__^U \$\endgroup\$
    – Charlie
    Commented Jul 22, 2020 at 11:59
  • 1
    \$\begingroup\$ I had similar problems myself with newlines not being counted properly by that Python code. Here is another way to do it. \$\endgroup\$
    – Dingus
    Commented Jul 22, 2020 at 12:05
2
\$\begingroup\$

PowerShell, 1 byte

0

Try it online!

This will work with every single digit. It just gets echoed.

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

Vim, 2 bytes

ii

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ This is a V submission. a Vim submission would require an esc at the end. \$\endgroup\$
    – Razetime
    Commented Apr 15, 2021 at 14:28
  • \$\begingroup\$ @Razetime Why would an esc be required? Even though Vim is still in Insert mode, the intended text has still been outputted. esc would just be moving it out of insert mode and into normal mode, but as far as I can tell, there is no requirement for a Vim submission to end in normal mode, or that it can't end in insert mode. \$\endgroup\$ Commented Apr 15, 2021 at 14:46
  • \$\begingroup\$ I'm not sure, but it's the same reason why 2i2i is a V quine rather than a vim quine \$\endgroup\$
    – Razetime
    Commented Apr 15, 2021 at 15:43
  • \$\begingroup\$ I think a consensus on this would be good. \$\endgroup\$ Commented Apr 15, 2021 at 16:46
  • \$\begingroup\$ 2i2I does not double the input until <esc> is pressed, that's why it's required there. This answer should be fine \$\endgroup\$
    – Leo
    Commented Apr 15, 2021 at 22:03
2
\$\begingroup\$

vim, 3 bytes

Four versions; one for each of the "insert on the same line" commands: A, a, i, and I:

  • A.<ESC>
  • a><ESC>
  • iB<ESC>
  • I2<ESC>

<ESC> is 0x1b.

(We can shave off a byte if we're willing to terminate while still in insert mode.)

Try it online! (For the A.<ESC> variant)

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

Lenguage, 4 bytes

����

Is this the first time where a lenguage program is totally written?

brainfuck, 14 bytes

+[+++++>+<]>.�

Try it online!

Forcefix average

\$\endgroup\$
5
  • \$\begingroup\$ 13 bytes BF with proof of average \$\endgroup\$
    – Bubbler
    Commented Apr 14, 2021 at 4:00
  • \$\begingroup\$ @Bubbler en.wikipedia.org/wiki/ASCII says ASCII is 0-127 \$\endgroup\$
    – l4m2
    Commented Apr 14, 2021 at 18:12
  • \$\begingroup\$ Please provide a hexdump for programs containing unprintables. I can tell that your Lenguage program is made entirely out of null bytes, but please specify next time, as they show up as �. \$\endgroup\$
    – Makonede
    Commented Apr 14, 2021 at 22:39
  • \$\begingroup\$ @Makonede Do &#0; in every browser show ? \$\endgroup\$
    – l4m2
    Commented Apr 15, 2021 at 1:50
  • \$\begingroup\$ Pretty sure yeah. I don't think it's browser-dependent though, as searching it shows that it is shown as the real U+FFFD. \$\endgroup\$
    – Makonede
    Commented Apr 15, 2021 at 2:06
2
\$\begingroup\$

Sandwich, 2 bytes

pp

Sandwich is comprised of opcodes. The first letter of each line is the opcode, and the rest of the line is the arguments.

The opcode for this code is p, which means print. The arguments are also p, which is passed to the opcode.

The result of this code is that it prints out p, AKA ASCII 112.

Since the code is 2 bytes long, simply using the same two characters will give the average of the entire code segment.

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

Plumber, 191 bytes

[]
]
]
...88 more ']'...
]
]
][[=

Try it online!

Drops a packet down, which is incremented up to 92 and printed. 92 is the ASCII value for \.

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

perl -Mfeature=say, 6 bytes

say"P"

Try it online!

I wrote a small program to find the program above:

#!/opt/perl/bin/perl

use 5.028;

use strict;
use warnings;
no  warnings 'syntax';

use experimental 'signatures';
use experimental 'lexical_subs';

for my $c (32 .. 127) {
    my $char = chr $c;
    my $s = qq [say"$char"];
    my $score = 0;
    $score += ord $_ for split // => $s;
    $score = int (.5 + $score / length ($s));
    if ($score == $c) {
        say $s;
    }
}

__END__

Try it online!

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

AWK, 15 bytes

BEGIN{print"X"}

Try it online!

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

Bash, 6 bytes

echo Y

Try it online!

echo Z

works as well.

Just like my Perl solution, this was found using a brute force search.

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

7, 1 byte

3

Try it online!

After the code cycles, 3 tries to output itself. However, 3 is an unnamed command, so it can't be directly outputted. Instead, it gets turned into 73, which is outputted. 7 specifies the output format as "the same as the input", so the 3 after it prints 3.


A more interesting solution:

7, 3 bytes (8 characters)

01116743

Try it online!

This outputs 3 as a number.

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

naz, 8 bytes

8a9m7a1o

Short and sweet. Verify the average character here.

Explanation

8a9m7a # Set the register to a value of 79
1o     # Output once
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 16 bytes

console.log('U')

Here's how I brute-forced the solution:

const average = str => String.fromCharCode([...str]
  .reduce((total, char) => (total += char
    .charCodeAt(0)) || total, 0) / str.length);

for (let i = 0; i < 2 ** 16; i++) {
  const char = String.fromCharCode(i);
  const avg = average(`console.log('${char}')`);
  char == avg && console.log({ code: i, char, avg });
  // { i: 85, char: 'U', avg: 'U' }
}

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

99, 22 bytes

99 99 9
99 99 9
9 99
9m

Try it online!

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

T-SQL, 8 bytes

PRINT'D'

DB Fiddle

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

Rust, 23 bytes

fn main(){print!("Q");}

Used modified Perl answer's script.

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

SimpleTemplate, 10 bytes

This is a simple code that outputs the average character itself:

{@echo"X"}

The average was calculated previously using JavaScript:

var code = '{@echo"X"}';

var sum = code.split('').reduce((a,_)=>a+_.charCodeAt(0), 0);

document.body.innerHTML += String.fromCharCode(sum/code.length);

To get the right character, I just used a random one, until I got one where the result was the same (in this case, "X").

The rules don't specify that the character can't be pre-calculated before. They only specify that you can't read the source code or that you can't take any input.


SimpleTemplate, 1 byte

Just simply outputs the character average:

1

This generates a PHP echo (example below):

echo (<<<'DATA6229934543895a3c69912b6e6658160198d45280523b34711abdc124745'
1
DATA6229934543895a3c69912b6e6658160198d45280523b34711abdc124745
);


You can try both versions on: http://sandbox.onlinephpfunctions.com/code/36449dad6c5de136ae489bcbeaddd97a6a7124bc

You can change the line 1048 to test the desired code.

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

Python 3, 11 bytes

print('K')#

The average value is 75.09, which rounds to 75 - K.

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

PHP, 1 byte

A

Try it online!

This will just output itself. Works with almost all of the printable ASCII characters.

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

Whitespace, 13 bytes

Outputs Data Link Escape (0x16)

I can't figure out how to put raw whitespace into a code block, so here you go:

[space][space][space][tab][space][tab][tab][space][lf]
[tab][lf]
[space][space]

Try it online

Explanation:

[space][space][space]           Push a number onto the stack
[tab][space][tab][tab][space]   Have that number be 22 (0x16)
[lf]                            End instruction
[tab][lf][space][space]         Output character from stack

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

Ada (GNAT), 53 bytes

procedure GNAT.IO.F is begin Put("M"); end GNAT.IO.F;

Try it online!

Why not?

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

C (gcc), 15 bytes

A(){return 84;}

Average is T. Returns ASCII code.

Try it online!

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

C - 18 chars

main(){puts("S");}
\$\endgroup\$
1
\$\begingroup\$

Scala, 10 bytes

print("N")

Online demo

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

PowerShell, 3 bytes

+$A

Try it online!

(43+36+65)/3 == 144/3 == 48 == ASCII('0')

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

bitch, 2 bytes

/1

Try it online! or verify the average.

Following up my COW answer with one in bitch . . . read nothing into that. Outputs 0 (average 48 exactly). / outputs the accumulator value (initialised to zero); 1 is a no-op here.

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

Rust, 5 bytes

||'R'

Try it online, Verify

Straightforward solution, just a closure that implements Fn() -> char.

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

1+ (with NOP), 3 bytes

'1:

Try it online! Verify!

1+ (without NOP), 4 bytes

11+:

Try it online! Verify!

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

MAWP v1.1, 6 bytes

33W/:.

I tried a different approach from Dion's answer.

Try it!, Verify

\$\endgroup\$
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.