TransWikia.com

Average of your code

Code Golf Asked by Dion on October 27, 2021

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!

74 Answers

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 .

Answered by Aaroneous Miller on October 27, 2021

Zsh, 5 bytes

<<< 5

Try it online!

Answered by pxeger on October 27, 2021

Vyxal, 1 byte

7

Try it Online!

Somehow the average of this program turns out to be the luckiest number in universe

Answered by wasif on October 27, 2021

Excel, 2 bytes

51

For once, there isn't some overly long Excel function to get in the way.

Answered by Axuary on October 27, 2021

MATLAB/Octave, 4 bytes

''''

Try it online!
Outputs single ' character. And basically it's the way to write that as an immidiate value - the outer apostrofes are to indicate the value is char array so we're left with '' which is escape code for a single apostrofe.

Answered by elementiro on October 27, 2021

Vyxal, 1 byte

0

Try it Online!

Any digit works.

Or, if you think that doesn't count,

\

Try it Online!

is a single-character string.

Answered by emanresu A on October 27, 2021

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.

Answered by Y45HK4R4ND1K4R on October 27, 2021

Answered by cnamejj on October 27, 2021

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)

Answered by Ray on October 27, 2021

Grok, 4 bytes

I[Wq

Characters are 73 91 87 113. Average is 91, which is [.

Answered by Aaroneous Miller on October 27, 2021

Ruby, 4 bytes


p 3

Try it online!

Adding a leading newline to lower the average from capital-letter land to digit land.

Answered by histocrat on October 27, 2021

Lenguage, 4 bytes

����

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

brainfuck, 14 bytes

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

Try it online!

Forcefix average

Answered by l4m2 on October 27, 2021

Vim, 2 bytes

ii

Try it online!

Answered by Aaroneous Miller on October 27, 2021

Answered by wasif on October 27, 2021

Keg, 1 byte

e

Try it online!

Haha. I'm not gonna be late to the party!

Answered by lyxal on October 27, 2021

1+ (with NOP), 3 bytes

'1:

Try it online! Verify!

1+ (without NOP), 4 bytes

11+:

Try it online! Verify!

Answered by null on October 27, 2021

MAWP v1.1, 6 bytes

33W/:.

I tried a different approach from Dion's answer.

Try it!, Verify

Answered by Razetime on October 27, 2021

Rust, 5 bytes

||'R'

Try it online, Verify

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

Answered by TehPers on October 27, 2021

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.

Answered by Dingus on October 27, 2021

Pyth, 2 bytes

"!

ASCII value of " is 34, and ASCII value of ! is 33.

(33 + 34) / 2 = 33.5 = 33

Try it online!

Answered by Viraxor on October 27, 2021

PowerShell, 1 byte

0

Try it online!

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

Answered by stackprotector on October 27, 2021

PowerShell, 3 bytes

+$A

Try it online!

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

Answered by mazzy on October 27, 2021

Scala, 10 bytes

print("N")

Online demo

Answered by user on October 27, 2021

C - 18 chars

main(){puts("S");}

Answered by AShelly on October 27, 2021

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.

Answered by Charlie on October 27, 2021

MATLAB... and MS-DOS and Bash? 7 bytes

!echo P

Outputs P. Length verification


First post here.

I thought this was going to be easy with MATLAB, as you can just enter a single digit number and it will return that as-is. Except that MATLAB prints more than just the number back out...

>> 0
ans =
     0

Same goes for strings.

>> 'a'
ans =
    'a'

Now I might've just waved my hands and said good enough, but where's the fun in that? :)

The only methods I knew of, that can print something to console without the extra ans = would be to use the fprintf() or disp() functions.

12 Bytes.

>> fprintf('T')
T

9 Bytes. Note, Disp(0) and other single-digit variations will not work due to average length constraint.

>> disp('J')
J

These two are valid submissions, but I kept wondering... Can I do better?

Then I learned I could send commands to the operating system with the Shell Escape Function. i.e. !COMMAND

Except the command is sent to whatever operating system that machine is running on. Since MATLAB is available for both Windows and Unix, whatever command I choose needs to work on both; ensuring that my code runs on all machines.

This more or less locked me to the single command; echo. (Kinda anti-climactic, really)

A few trials and error with the output character, and I converged to the final answer. 7 Bytes.

>> !echo P
P 

I really hope this isn't breaking any rules here...

Answered by mimocha on October 27, 2021

C (gcc), 15 bytes

A(){return 84;}

Average is T. Returns ASCII code.

Try it online!

Answered by S.S. Anne on October 27, 2021

Ada (GNAT), 53 bytes

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

Try it online!

Why not?

Answered by LambdaBeta on October 27, 2021

><>, 4 bytes

" o;

Try it online!

Outputs ; using a looping string literal.

Answered by SE - stop firing the good guys on October 27, 2021

cat, 1 byte

a

If your challenge can be competitively solved with cat, there is probably something wrong with the challenge.

Answered by user253751 on October 27, 2021

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

Answered by Ethan Chapman on October 27, 2021

PHP, 1 byte

A

Try it online!

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

Answered by The random guy on October 27, 2021

COW, 11 8 bytes

BOOM!!


Try it online! or verify the average.

Explosively prints 0 (average 48.375), using the fact that all non-instructions (B, !!, and the two newlines) are no-ops.

6 bytes

I'm retaining the 8-byte version above because it's my favourite, but here's a 6-byter thanks to @JoKing:

OOM!        

(Note the code ends with two tabs.) Also prints 0 (average 47.66...).

And here's another, with an average of exactly 48:

OOM!



COW, 87 78 bytes

MoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMOOMMMMOOmoOMoOmOoMOomooMMMMOomoomoOMoo

Try it online! or verify the average.

No no-ops this time. Prints [ (average 90.54...) by counting up to 91.

Commented

MoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoO    push 13 to first memory block
MOO        begin outer loop
  MMM      copy value to register
  MOO      begin inner loop
    moO    switch to second memory block
    MoO    increment                                  
    mOo    switch to first memory block
    MOo    decrement
  moo      end inner loop
  MMM      paste register value into first memory block
  MOo      decrement
moo        end outer loop
moO        switch to second memory block
Moo        print as ASCII character

Answered by Dingus on October 27, 2021

Ruby, 6 bytes

$><<?8

average is 56.16

$ cat mean.rb
$><<?8
$ ruby mean.rb
8
$ python -c "[print(chr(int(0.5 + sum([ord(c) for c in s])/len(s)))) for s in [l.strip() for l in open('mean.rb')]]"
8

Answered by AShelly on October 27, 2021

Python 3, 10 bytes

print("N")

Try it online!

Answered by Dion on October 27, 2021

Java (JDK), 4 bytes

A->9

Try it online!

Outputs the number 9 which as a character is the average character of the code.

Defined as a int f(Void v). Note that Void is an accepted argument type for "no input" challenges.

If an int isn't an acceptable output for this challenge, the following is most definitely valid, for an extra byte.

A->56

Try it online!

Outputs 8 which is the average character.

Defined as a char f(Void v). I was surprised that no casting to char was required, but it beats v->';' by one byte.

Answered by Olivier Grégoire on October 27, 2021

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.

Answered by Neil on October 27, 2021

Z80Golf, 4 bytes

00000000: 3e 91 ff 76   

Try it online!

This is ld a, 0x91rst 0x38 (putchar) → halt.

It prints a single 0x91 byte, and (0x3e + 0x91 + 0xff + 0x76) / 4 = 0x91.

Answered by Lynn on October 27, 2021

Python 3, 11 bytes

print('K')#

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

Answered by MLavrentyev on October 27, 2021

SuperMarioLang, 2 bytes

:%

Try it online!

A simple answer in SuperMarioLang. The only command that gets executed is : which prints the value of the currently pointed memory position (0) as a number (initially 0). The second command % never gets executed as Mario (the command pointer) falls because there is no floor under him, so the program stops. The average between : and % is 0.

Also works in MarioLANG, where % is just interpreted as a comment.

Answered by Charlie on October 27, 2021

Rust, 23 bytes

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

Used modified Perl answer's script.

Answered by Vi. on October 27, 2021

Malbolge, 17 bytes

(=<;:9876543210TA

Try it online!

Test the code average

Explanation:

(=<;:9876543210TA

(                    Copies the value pointed by register d to register d.
                     (d = 0 so it copies the '(' ASCII value)
 =<;:9876543210      Succesive calls to the crazy operation to update the value
                     in register a until the value stored meets the code average
               T     Prints the value in a
                A    Ends the program

Luckily this time we don't need to cope with Malbolge's code encryption feature.

Answered by Charlie on October 27, 2021

Perl 5 + -M5.10.0, 67 bytes

I know this isn't the shortest, but I think it's what OP was after.

$_=q{$_="$_=q{$_};eval";$-+=ord for/./g;say chr(.5+$-/y///c)};eval

Try it online!

Answered by Dom Hastings on October 27, 2021

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.

Answered by Ismael Miguel on October 27, 2021

Lost, 19 13 11 bytes

v<<<<
>%(9@

-6 bytes thanks to @JoKing.

The average is 57.090..., which will be rounded to 57 (character '9').

Try it online or verify that it's deterministic.

Explanation:

Explanation of the language in general:

Lost is a 2D path-walking language. Most 2D path-walking languages start at the top-left position and travel towards the right by default. Lost is unique however, in that both the start position AND starting direction it travels in is completely random. So making the program deterministic, meaning it will have the same output regardless of where it starts or travels, can be quite tricky.

A Lost program of 2 rows and 5 characters per row can have 40 possible program flows. It can start on any one of the 10 characters in the program, and it can start traveling up/north, down/south, left/west, or right/east.

In Lost you therefore want to lead everything to a starting position, so it'll follow the designed path you want it to. In addition, you'll usually have to clean the stack when it starts somewhere in the middle.

Explanation of the program:

All arrows will lead the path towards the leading > on the second line. From there the program flow is as follows:

  • >: travel in an east/right direction
  • %: Put the safety 'off'. In a Lost program, an @ will terminate the program, but only when the safety is 'off'. When the program starts, the safety is always 'on' by default, otherwise a program flow starting at the exit character @ would immediately terminate without doing anything. The % will turn this safety 'off', so when we now encounter an @ the program will terminate (if the safety is still 'on', the @ will be a no-op instead).
  • (: Pop the top value, and push it to the scope. This is basically used to make the stack empty if we started somewhere in the middle.
  • 9: Push a 9
  • @: Terminate the program if the safety is 'off' (which it is at this point). After which all the values on the stack will be output implicitly. So it'll output the 9 for the average character of unicode 57.

Answered by Kevin Cruijssen on October 27, 2021

05AB1E, 1 byte

0

Or any other single digit.

Try it online.

Answered by Kevin Cruijssen on October 27, 2021

Pyramid Scheme, 29 bytes

   ^
  / 
 /out
^-----
-

 

Try it online!

This has an average of ~48.137, so this outputs 0.

If we're allowed a trailing newline, then we can get 4 bytes

^,
-

Try it online!

Again, this outputs 0, but by printing the return of the pyramid, which has an extra newline

Answered by Jo King on October 27, 2021

T-SQL, 8 bytes

PRINT'D'

DB Fiddle

Answered by Razvan Socol on October 27, 2021

HQ9+ Family, 1 bytes

Q

Believe it or not, there are challenges can be solved by HQ9+ in 2020.

Answered by tsh on October 27, 2021

99, 22 bytes

99 99 9
99 99 9
9 99
9m

Try it online!

Answered by PkmnQ on October 27, 2021

AlphaBeta, 10 bytes

edaaaaaCLz

Try it online!

Answered by PkmnQ on October 27, 2021

Answered by Mukundan314 on October 27, 2021

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' }
}

Answered by GirkovArpa on October 27, 2021

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

Answered by sporeball on October 27, 2021

Hexagony, 3 bytes

0!@

Try it online! or Check the average character

Explanation

0     Set current memory edge to 0
 !    Output current memory edge as a number
  @   Terminate the program

Answered by Mukundan314 on October 27, 2021

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.

Answered by Pizgenal Filegav on October 27, 2021

Backhand, 6 bytes

"o@7" 

Try it online!

Explanation

"  7   Start a quote, then the character 7
 o  "  (backwards) End a quote, output the character
  @    Stop the program + no-op.

Answered by user92069 on October 27, 2021

brainfuck, 15 bytes

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

Try it online!

Requires an 8 bit interpreter as it uses modulo 256 arithmetic.

Average is 53.4. Outputs 5 which is character 53.

Answered by Level River St on October 27, 2021

brainfuck, 16 bytes

Outputs 6, the average of the program.

-[>+<-----]>+++.

Try it online!

Answered by user92069 on October 27, 2021

Io, 8 bytes

I feel that this is too boring to find...

"Y"print

Try it online!

Answered by user92069 on October 27, 2021

Gol><>, 3 bytes

`TH

Try it online! and Check the average character.

`T   Escape the char T and push its charcode to stack
  H  Print all values on the stack and halt

Found by making the template `<any char>H and repeating the average calculator until it converges.

Answered by Bubbler on October 27, 2021

Unreadable, 111 bytes

'"'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'"""

Try it online! or Check average

Note that the average character is $ and does not appear in the source code, which contains only 37 's and 74 "s.

Answered by Robin Ryder on October 27, 2021

SQLite, 9 bytes

SELECT"@"

Try it online!

Answered by Abigail on October 27, 2021

R, 8 bytes

cat("B")

Try it online! or Check average

or

cat('C')

Try it online! or Check average

Also,

cat(8)

(with 2 null bytes) doesn't work on TIO, but works in RStudio on my Kubuntu machine.

Try it online! or Check average.

Answered by Robin Ryder on October 27, 2021

AWK, 15 bytes

BEGIN{print"X"}

Try it online!

Answered by Abigail on October 27, 2021

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.

Answered by Abigail on October 27, 2021

Befunge-93, 5 3 bytes

.@!

Try it online!

.@ prints 0. ! was added so the average is 47.667, which rounds to 48, which is the ASCII code for 0.

Answered by Abigail on October 27, 2021

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!

Answered by Abigail on October 27, 2021

JavaScript (Browser), 10 bytes

alert('M')

Try it online!

Answered by Matthew Jensen on October 27, 2021

Retina 0.8.2, 1 byte

0

Try it online! Works by counting the number of 0s in the input. This can of course be extended to any arbitrary number of bytes just by repeating the number of 0s, or substituting other characters which happen to have an average byte value of 0, e.g. .2, as long as the result remains a valid regular expression that doesn't match the empty string.

Without using a Match (count) stage, I think the minimum possible is 3 bytes:

^
4

This program outputs 4, whose ASCII code 52 is the average of 94 (for ^) and 10 (for the newline).

Answered by Neil on October 27, 2021

MAWP 0.1, 3 bytes

99:

Outputs 9. Works because : and 9 are neighbours in the table, so 9: gives a value thats in between them, so adding another 9 guarantees that the average corresponds to 9

Answered by Dion on October 27, 2021

APL (Dyalog Unicode) and lots of other languages and REPLs, 1 byte

4

Try it online!


However, more interesting is:

''''

Which actually evaluates to the single quote. Try it online!

Answered by Adám on October 27, 2021

///, 1 byte

A

Try it online!

As it is 1 byte, simply outputting the source code gives the average. /// just outputs the source code unchanged if it doesn’t contain any / characters, so almost any ASCII character works in place of A

Answered by caird coinheringaahing on October 27, 2021

Answered by fireflame241 on October 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP