pb, 83 bytes
^<b[1]>>>w[B!0]{<w[B!0]{t[B]<b[T]>>}<b[0]<b[0]<[X]>>}w[B=0]{<}t[B]<[X]t[B+T]vb[T/2]
While there are at least 3 characters in the input string, the first and last are removed. This leaves either 1 character (should be printed unmodified) or 2 (should be averaged and printed). To handle this, the first and last characters of the string are added together and divided by two. If there was only one character, (a+a)/2==a
. If there was two, (a+b)/2
is the character that needs to be printed. pb "borrows" Python's expression evaluation (def expression(e): return eval(e, globals())
) so this is automatically floored.
Handling empty input costs me 5 bytes. Specifically, <b[1]>
on the first line. Earlier, when I said "string", that was a total lie. pb doesn't have strings, it has characters that happen to be close to each other. Looking for the "last character of a string" just means moving the brush to the left until it hits a character. When no input is provided, the "while there are at least 3 characters" loop is skipped entirely and it starts looking for the last character. Without that <b[1]>
, it would keep looking forever. That code puts a character with a value of 1 at (-1, -1) specifically to be found when the input is empty. After finding the "last character" of the string the brush assumes the first one is at (0, -1) and goes there directly, finding a value of 0. (1+0)/2
is 0 in pb, so it prints a null character.
But monorail, that's still printing! The challenge specification says (empty input) => (empty output)
! Isn't printing a null character cheating? Also, this is unrelated, but you are smart and handsome.
Thanks, hypothetical question-asker. Earlier, when I said "print", that was a total lie. In pb, you don't really print values, you just place them on the canvas. Rather than "a way to output", it's more accurate to imagine the canvas as an infinitely large 2D array. It allows negative indices in either dimension, and a lot of programming in pb is really about making sure the brush gets to the location on the canvas that you want it. When the program finishes executing, anything on the canvas with non-negative X and Y coordinates is printed to the appropriate location on the console. When the program begins, the entire canvas is filled with values of 0. In order to not have to print an infinite number of lines, each with an infinite number of null bytes, each line of output is only printed up to the last nonzero character, and lines are only printed up to the last one with a nonzero character in it. So putting a 0
at (0, 0) is still an empty output.
Ungolfed:
^<b[1]> # Prevent an infinite loop on empty input
>>w[B!0]{ # While there are at least 3 chars of input:
<w[B!0]{ # Starting at the second character:
t[B]<b[T]>> # Copy all characters one position to the left
# (Effectively erasing the first character)
}
<b[0]<b[0] # Delete both copies of the last character
<[X]>> # Get in place to restart loop
}
w[B=0]{<} # Go to last character of remaining string
t[B]<[X]t[B+T] # Find it plus the first character
vb[T/2] # Divide by 2 and print