25
\$\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\$
13
  • 4
    \$\begingroup\$ Average Character Calculator (only works for unicode characters input) \$\endgroup\$ Jul 19 '20 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
    Jul 19 '20 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
    Jul 19 '20 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\$ Jul 19 '20 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
    Jul 20 '20 at 6:25

74 Answers 74

23
\$\begingroup\$

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.

\$\endgroup\$
22
\$\begingroup\$

///, 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

\$\endgroup\$
7
  • 3
    \$\begingroup\$ +1 Right tool for the job. \$\endgroup\$
    – Adám
    Jul 19 '20 at 19:04
  • 4
    \$\begingroup\$ Could we do a CW of languages which can implicitly output a single character? Retina, Jelly, etc. \$\endgroup\$ Jul 19 '20 at 19:06
  • \$\begingroup\$ @fireflame241 Looks like Adàm may have Beat you to it \$\endgroup\$ Jul 19 '20 at 19:08
  • \$\begingroup\$ Also works for Charcoal. \$\endgroup\$
    – Neil
    Jul 19 '20 at 21:38
  • \$\begingroup\$ Also works for Keg/Canvas/Carrot. \$\endgroup\$
    – user92069
    Jul 20 '20 at 0:35
18
\$\begingroup\$

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.

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

HQ9+ Family, 1 bytes

Q

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

\$\endgroup\$
3
  • \$\begingroup\$ Don’t know whether this counts as indirectly or “directly reading own source code.” But I upvoted immediately for the clever meta-ness and original use of such a ‘done’ language! \$\endgroup\$
    – AviFS
    Jul 21 '20 at 10:41
  • \$\begingroup\$ This challenge can also be solved by cat in 2020. \$\endgroup\$
    – user253751
    Jul 22 '20 at 12:29
  • \$\begingroup\$ The fact that you can solve this challenge with HQ9+ and also with cat makes it a particularly lame challenge in 2020. \$\endgroup\$
    – user253751
    Jul 22 '20 at 12:30
13
\$\begingroup\$

Python 2, 8 bytes

print"Y"

Try it online!

Average Character Verified

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Also compatible with Lua \$\endgroup\$ Jul 20 '20 at 5:58
  • 2
    \$\begingroup\$ And with Perl too! \$\endgroup\$ Jul 20 '20 at 9:19
13
\$\begingroup\$

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!

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

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.

\$\endgroup\$
0
8
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ BOOM!! Out of mana! - Don't we know those lines? \$\endgroup\$
    – Arsenal
    Jul 22 '20 at 9:26
5
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

cat, 1 byte

a

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

\$\endgroup\$
2
  • \$\begingroup\$ Assuming cat is allowed, isn't this reading its own source code? \$\endgroup\$ Jul 21 '20 at 19:24
  • \$\begingroup\$ @Calculuswhiz No, cat is just the interpreter. Every interpreter has to read the source code of the program. There's no instruction in this program which reads the source code, only one that outputs the letter a. \$\endgroup\$
    – user253751
    Jul 21 '20 at 19:32
4
\$\begingroup\$

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

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

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).

\$\endgroup\$
2
  • \$\begingroup\$ If 0 outputs 0, what stops a .2 outputing 0 too? \$\endgroup\$
    – tsh
    Jul 20 '20 at 8:08
  • \$\begingroup\$ @tsh Yeah, I thought of that myself overnight, almost any two ASCII characters with an average of 0 will work. \$\endgroup\$
    – Neil
    Jul 20 '20 at 9:50
4
\$\begingroup\$

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!

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

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.
\$\endgroup\$
0
4
\$\begingroup\$

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...

\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.
\$\endgroup\$
3
\$\begingroup\$

Python 3, 10 bytes

print("N")

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ either print("O") or print('P') works too \$\endgroup\$ Jul 21 '20 at 11:10
  • \$\begingroup\$ A more fun idea is print(chr(82)). \$\endgroup\$ Apr 13 at 18:22
3
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ It's an implicit narrowing conversion; any constant integer expression whose value is between 0 and 65535 can be used where a char is expected. \$\endgroup\$
    – Neil
    Jul 23 '20 at 0:13
3
\$\begingroup\$

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
\$\endgroup\$
3
  • \$\begingroup\$ p ?E outputs "E", but I don't think it's acceptable. \$\endgroup\$ Jul 21 '20 at 13:16
  • \$\begingroup\$ oops, somehow I dropped the leading char when calculating the average. Fixed. \$\endgroup\$
    – AShelly
    Jul 21 '20 at 17:42
  • 1
    \$\begingroup\$ Your code works fine now. The average is still wrong, though. \$\endgroup\$ Jul 21 '20 at 21:02
2
\$\begingroup\$

SQLite, 9 bytes

SELECT"@"

Try it online!

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

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.

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

brainfuck, 16 bytes

Outputs 6, the average of the program.

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

Try it online!

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

JavaScript (Node.js), 5 bytes

()=>3

Try it online!

\$\endgroup\$
9
  • \$\begingroup\$ That returns 3, but outputs nothing. \$\endgroup\$
    – GirkovArpa
    Jul 20 '20 at 4:32
  • \$\begingroup\$ @GirkovArpa, Functions are allowed by default and function may output by return value \$\endgroup\$ Jul 20 '20 at 5:20
  • \$\begingroup\$ The example specifically says printed out. \$\endgroup\$
    – GirkovArpa
    Jul 20 '20 at 5:26
  • 1
    \$\begingroup\$ @GirkovArpa that would but that only works on a repl environment \$\endgroup\$ Jul 21 '20 at 17:01
  • 1
    \$\begingroup\$ @GirkovArpa the average of _=>3 would be C not 3 \$\endgroup\$ Jul 21 '20 at 17:04
2
\$\begingroup\$

JavaScript (Browser), 10 bytes

alert('M')

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 8 bytes. \$\endgroup\$
    – Bubbler
    Aug 6 '20 at 8:48
2
\$\begingroup\$

AlphaBeta, 10 bytes

edaaaaaCLz

Try it online!

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

05AB1E, 1 byte

0

Or any other single digit.

Try it online.

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

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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