Write two functions of one line each that can encode and decode base64.
- Use any language you want, but if the language can naturally be converted to a single line (e.g. javascript) then any block of code separated by an end-of-line character is counted as a line.
- The functions must be self-contained. They cannot reference outside variables or imported libraries, with the exception of a single input argument.
- Expanding on the previous point, you cannot define constants or variables for use inside the function. For instance, if your function references
alphabet
as the collection of base64 symbols, replace every instance ofalphabet
with a literal of the same value. - The functions must be one line each, not counting the function definition itself. For example, in languages like Visual Basic, functions are a minimum of three lines due to the beginning and end lines being a required part of the function definition. This is acceptable as long as the code that does the base64 conversion is only one line.
- Do not use built in functions such as javascript's
atob
orbtoa
.
Here's my functions written in python. I can't find a way to make these smaller.
def tobase64(m):
return ''.join(['ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'[int(z[::-1].zfill(6)[::-1],2)] for z in [''.join([bin(ord(x))[2:].zfill(8)for x in m])[y:y+6] for y in range(0,len(m)*8,6)]])+''.join(['=' for x in range(-len(m)%3)])
and the decoder
def fromb64(m):
return ''.join([chr(int(w,2)) for w in [(''.join([''.join([bin('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.index(x))[2:].zfill(6) for x in m if x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'])[y:y+8] for y in range(0,len(''.join([bin('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.index(x))[2:].zfill(6) for x in m if x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'])),8)])+'a')[:-(len(m)-len(m.replace('=','')))*2-1][z:z+8] for z in range(0,len(''.join([bin('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.index(x))[2:].zfill(6) for x in m if x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'])),8)] if len(w) == 8])
atob
andbtoa
. You may want to disallow builtin functions that do this task. \$\endgroup\$