Code Golf Asked by Dion on October 27, 2021
Your task is to output the average character of your code.
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
.
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!
[]
]
]
...88 more ']'...
]
]
][[=
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
Answered by pxeger on October 27, 2021
Answered by wasif on October 27, 2021
51
For once, there isn't some overly long Excel function to get in the way.
Answered by Axuary on October 27, 2021
''''
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
0
Any digit works.
Or, if you think that doesn't count,
\
is a single-character string.
Answered by emanresu A on October 27, 2021
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
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
Answered by Aaroneous Miller on October 27, 2021
p 3
Adding a leading newline to lower the average from capital-letter land to digit land.
Answered by histocrat on October 27, 2021
Is this the first time where a lenguage program is totally written?
+[+++++>+<]>.
Forcefix average
Answered by l4m2 on October 27, 2021
Answered by Aaroneous Miller on October 27, 2021
Answered by wasif on October 27, 2021
Answered by lyxal on October 27, 2021
'1:
11+:
Answered by null on October 27, 2021
Answered by Razetime on October 27, 2021
||'R'
Straightforward solution, just a closure that implements Fn() -> char
.
Answered by TehPers on October 27, 2021
/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
Answered by Viraxor on October 27, 2021
Answered by stackprotector on October 27, 2021
Answered by mazzy on October 27, 2021
Answered by user on October 27, 2021
main(){puts("S");}
Answered by AShelly on October 27, 2021
IT'S SHOWTIME
TALK TO THE HAND "A"
YOU HAVE BEEN TERMINATED
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
!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
Answered by S.S. Anne on October 27, 2021
Answered by LambdaBeta on October 27, 2021
Answered by SE - stop firing the good guys on October 27, 2021
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
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]
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
A
This will just output itself. Works with almost all of the printable ASCII characters.
Answered by The random guy on October 27, 2021
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.
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!
MoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMOOMMMMOOmoOMoOmOoMOomooMMMMOomoomoOMoo
Try it online! or verify the average.
No no-ops this time. Prints [
(average 90.54...) by counting up to 91.
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
$><<?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
Answered by Dion on October 27, 2021
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.
Answered by Olivier Grégoire on October 27, 2021
@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
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
.
Answered by Lynn on October 27, 2021
print('K')#
The average value is 75.09, which rounds to 75 - K.
Answered by MLavrentyev on October 27, 2021
:%
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
Answered by Vi. on October 27, 2021
(=<;: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.
Answered by Charlie on October 27, 2021
-M5.10.0
, 67 bytesI 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
Answered by Dom Hastings on October 27, 2021
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.
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
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 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
Answered by Kevin Cruijssen on October 27, 2021
^
/
/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
Answered by Jo King on October 27, 2021
Answered by tsh on October 27, 2021
Answered by PkmnQ on October 27, 2021
Answered by PkmnQ on October 27, 2021
Answered by Mukundan314 on October 27, 2021
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
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
0!@
Try it online! or Check the average character
0 Set current memory edge to 0
! Output current memory edge as a number
@ Terminate the program
Answered by Mukundan314 on October 27, 2021
3
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
.
01116743
This outputs 3
as a number.
Answered by Pizgenal Filegav on October 27, 2021
"o@7"
" 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
+[+++++>+<]>++.
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
Answered by user92069 on October 27, 2021
Answered by user92069 on October 27, 2021
`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
'"'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'""'"""
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
Answered by Abigail on October 27, 2021
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.
Answered by Robin Ryder on October 27, 2021
Answered by Abigail on October 27, 2021
echo Y
echo Z
Just like my Perl solution, this was found using a brute force search.
Answered by Abigail on October 27, 2021
.@!
.@
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
say"P"
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__
Answered by Abigail on October 27, 2021
Answered by Matthew Jensen on October 27, 2021
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).
Answered by Neil on October 27, 2021
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
4
However, more interesting is:
''''
Which actually evaluates to the single quote. Try it online!
Answered by Adám on October 27, 2021
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
Answered by caird coinheringaahing on October 27, 2021
Answered by fireflame241 on October 27, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP