s:$:__:
s:...:& :g
s: _*$::
Try it online!
It gets a bit tricky regarding the empty string input, since sed has no meaning of a falsy value. So to deal with this, I provide you with two possible interpretations of the rules to validate my submission:
A. You essentially provide nothing as input, not even a trailing newline (as it is the case with all the examples, including that 6 Mb file).
Usage:
echo -n ""|sed -f script
Output: nothing is printed, because sed doesn't even run the script without input.
B. One could consider as falsy value for sed to be an unique string, i.e., returned only when the input is an empty string.
Usage:
echo ""|sed -f script
Output:
__
I prefer the first interpretation so much more, as I believe it to be the closest to the intended rule, but the last one helps if you run the script using that TIO link.
10Why special case the empty string? May we choose which character to use for the padding? – Shaggy – 2019-05-19T14:49:57.557
Is null considered a falsey value? – Luis felipe De jesus Munoz – 2019-05-19T14:58:58.147
2@LuisfelipeDejesusMunoz, falsey values are defined by the language, not the challenge author. – Shaggy – 2019-05-19T15:00:38.160
12So the task is actually: Given a string, return a falsey value if it is empty, otherwise split it into groups of three, padding with underscores if necessary? Seems like an odd pair-up of two unrelated tasks (checking string length and splitting). – Adám – 2019-05-19T15:27:44.410
15Things to avoid: Exceptional edge cases. In this case, an empty string should returnan empty array or equivalent, not a falsey value – Jo King – 2019-05-19T22:05:25.930
6Lots of statically-typed languages cannot return more than one type from a function – Embodiment of Ignorance – 2019-05-19T23:30:03.253
@JoKing But in Python (and likely many other languages), an empty array is Falsey, so this is not necessarily a complication at all. – manassehkatz-Moving 2 Codidact – 2019-05-20T16:33:07.160
4@manassehkatz Yes, but only in those languages. In some languages it makes solutions far more complicated, or even impossible (e.g statically typed languages). – Jo King – 2019-05-20T22:01:57.927
@JoKing In Java a return type of
Object
works, in Cvoid *
(or another pointer type), etc. – Jakob – 2019-05-25T04:14:49.6201@JoKing, as I understand I have to return either empty array or falsy value, so there is no special edge case. – Qwertiy – 2019-05-25T21:53:46.690
@Qwertiy That has since been edited in – Jo King – 2019-05-26T04:12:04.627