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!
77 Answers 77
///, 1 byte
A
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
-
3\$\begingroup\$ +1 Right tool for the job. \$\endgroup\$Adám– Adám2020年07月19日 19:04:10 +00:00Commented Jul 19, 2020 at 19:04
-
5\$\begingroup\$ Could we do a CW of languages which can implicitly output a single character? Retina, Jelly, etc. \$\endgroup\$fireflame241– fireflame2412020年07月19日 19:06:48 +00:00Commented Jul 19, 2020 at 19:06
-
\$\begingroup\$ @fireflame241 Looks like Adàm may have Beat you to it \$\endgroup\$2020年07月19日 19:08:19 +00:00Commented Jul 19, 2020 at 19:08
-
\$\begingroup\$ Also works for Charcoal. \$\endgroup\$Neil– Neil2020年07月19日 21:38:22 +00:00Commented Jul 19, 2020 at 21:38
-
\$\begingroup\$ Also works for Keg/Canvas/Carrot. \$\endgroup\$user92069– user920692020年07月20日 00:35:36 +00:00Commented Jul 20, 2020 at 0:35
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.
Malbolge, 17 bytes
(=<;:9876543210TA
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.
-
\$\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– AviFS2020年07月21日 10:41:03 +00:00Commented Jul 21, 2020 at 10:41
-
1\$\begingroup\$ Also compatible with Lua \$\endgroup\$val - disappointed in SE– val - disappointed in SE2020年07月20日 05:58:29 +00:00Commented Jul 20, 2020 at 5:58
-
2\$\begingroup\$ And with Perl too! \$\endgroup\$Dom Hastings– Dom Hastings2020年07月20日 09:19:33 +00:00Commented Jul 20, 2020 at 9:19
APL (Dyalog Unicode) and lots of other languages and REPLs, 1 byte
4
However, more interesting is:
''''
Which actually evaluates to the single quote. Try it online!
brainfuck, 15 bytes
+[+++++>+<]>++.
Requires an 8 bit interpreter as it uses modulo 256 arithmetic.
Average is 53.4. Outputs 5
which is character 53.
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
-
\$\begingroup\$ BOOM!! Out of mana! - Don't we know those lines? \$\endgroup\$Arsenal– Arsenal2020年07月22日 09:26:28 +00:00Commented Jul 22, 2020 at 9:26
cat, 1 byte
a
If your challenge can be competitively solved with cat
, there is probably something wrong with the challenge.
-
\$\begingroup\$ Assuming
cat
is allowed, isn't this reading its own source code? \$\endgroup\$General Grievance– General Grievance2020年07月21日 19:24:31 +00:00Commented Jul 21, 2020 at 19:24 -
\$\begingroup\$ @GeneralGrievance No.
a
is a cat instruction which is the equivalent ofprint("a")
in other languages. It's not an equivalent toprint(open(__file__).read())
. \$\endgroup\$Stack Exchange Broke The Law– Stack Exchange Broke The Law2025年08月06日 14:16:26 +00:00Commented Aug 6 at 14:16
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.
Pyramid Scheme, 29 bytes
^
/ \
/out\
^-----
-
This has an average of ~48.137, so this outputs 0
.
If we're allowed a trailing newline, then we can get 4 bytes
^,
-
Again, this outputs 0
, but by printing the return of the pyramid, which has an extra newline
Retina 0.8.2, 1 byte
0
Try it online! Works by counting the number of 0
s in the input. This can of course be extended to any arbitrary number of bytes just by repeating the number of 0
s, 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).
-
\$\begingroup\$ If
0
outputs0
, what stops a.2
outputing0
too? \$\endgroup\$tsh– tsh2020年07月20日 08:08:40 +00:00Commented Jul 20, 2020 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– Neil2020年07月20日 09:50:56 +00:00Commented Jul 20, 2020 at 9:50
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
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 a9
@
: 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 the9
for the average character of unicode57
.
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...
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
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
SuperMarioLang, 2 bytes
:%
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.
Backhand, 6 bytes
"o@7"
Explanation
" 7 Start a quote, then the character 7
o " (backwards) End a quote, output the character
@ Stop the program + no-op.
-
\$\begingroup\$ either
print("O")
orprint('P')
works too \$\endgroup\$Gábor Fekete– Gábor Fekete2020年07月21日 11:10:45 +00:00Commented Jul 21, 2020 at 11:10 -
\$\begingroup\$ A more fun idea is
print(chr(82))
. \$\endgroup\$user100887– user1008872021年04月13日 18:22:32 +00:00Commented Apr 13, 2021 at 18:22
Java (JDK), 4 bytes
A->9
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
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.
-
\$\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– Neil2020年07月23日 00:13:39 +00:00Commented Jul 23, 2020 at 0:13
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
-
\$\begingroup\$
p ?E
outputs"E"
, but I don't think it's acceptable. \$\endgroup\$Eric Duminil– Eric Duminil2020年07月21日 13:16:31 +00:00Commented Jul 21, 2020 at 13:16 -
\$\begingroup\$ oops, somehow I dropped the leading char when calculating the average. Fixed. \$\endgroup\$AShelly– AShelly2020年07月21日 17:42:51 +00:00Commented Jul 21, 2020 at 17:42
-
1\$\begingroup\$ Your code works fine now. The average is still wrong, though. \$\endgroup\$Eric Duminil– Eric Duminil2020年07月21日 21:02:56 +00:00Commented Jul 21, 2020 at 21:02
Befunge-93, (削除) 5 (削除ここまで) 3 bytes
.@!
.@
prints 0
. !
was added so the average is 47.667
, which rounds to 48
, which is the ASCII code for 0
.
-
\$\begingroup\$ That returns 3, but outputs nothing. \$\endgroup\$GirkovArpa– GirkovArpa2020年07月20日 04:32:35 +00:00Commented Jul 20, 2020 at 4:32
-
\$\begingroup\$ @GirkovArpa, Functions are allowed by default and function may output by return value \$\endgroup\$Mukundan314– Mukundan3142020年07月20日 05:20:54 +00:00Commented Jul 20, 2020 at 5:20
-
\$\begingroup\$ The example specifically says
printed out
. \$\endgroup\$GirkovArpa– GirkovArpa2020年07月20日 05:26:25 +00:00Commented Jul 20, 2020 at 5:26 -
1\$\begingroup\$ @GirkovArpa that would but that only works on a repl environment \$\endgroup\$Mukundan314– Mukundan3142020年07月21日 17:01:29 +00:00Commented Jul 21, 2020 at 17:01
-
1\$\begingroup\$ @GirkovArpa the average of
_=>3
would beC
not3
\$\endgroup\$Mukundan314– Mukundan3142020年07月21日 17:04:16 +00:00Commented Jul 21, 2020 at 17:04
Z80Golf, 4 bytes
00000000: 3e 91 ff 76
This is ld a, 0x91
→ rst 0x38
(putchar) → halt
.
It prints a single 0x91
byte, and (0x3e + 0x91 + 0xff + 0x76) / 4 = 0x91
.
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
isy * z = x
, one may argue output any single character should be allowed. \$\endgroup\$