1
\$\begingroup\$

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 of alphabet 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 or btoa.

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])
\$\endgroup\$
3
  • \$\begingroup\$ JavaScript, for one, has built-in functions that do exactly that, atob and btoa. You may want to disallow builtin functions that do this task. \$\endgroup\$ Commented Apr 15, 2015 at 22:14
  • \$\begingroup\$ @NinjaBearMonkey Edited the post. Thanks! \$\endgroup\$
    – Daffy
    Commented Apr 15, 2015 at 22:17
  • \$\begingroup\$ Also, which base64 variant? \$\endgroup\$ Commented Apr 15, 2015 at 22:29

1 Answer 1

1
\$\begingroup\$

Python 2, 437 characters

As a first improvement to your Python solution, you can cut down on the references to the long string of characters 'abc...ABC...0123456789+/':

def fromb64(m):
    return (lambda A:''.join([chr(int(w,2)) for w in [(''.join([''.join([bin(A.index(x))[2:].zfill(6) for x in m if x in A])[y:y+8] for y in range(0,len(''.join([bin(A.index(x))[2:].zfill(6) for x in m if x in A])),8)])+'a')[:-(len(m)-len(m.replace('=','')))*2-1][z:z+8] for z in range(0,len(''.join([bin(A.index(x))[2:].zfill(6) for x in m if x in A])),8)] if len(w)==8]))('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/')

This is just (lambda A: {{what you did, but with that string replaced by A}})(that string).

\$\endgroup\$
1
  • \$\begingroup\$ Very clever! I didn't think to use lambda functions. \$\endgroup\$
    – Daffy
    Commented Apr 16, 2015 at 1:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.