Compared against declared_pairs · told 12 of 12 deliberately broken copies apart · 2006 ms
def tweak(ch):
if ch == '\n':
return '\n'
if ch == '\r':
return '\r'
if ch == '\t':
return '\t'
if ch == ' ':
return ' '
if ch == '%':
return '%'
if ch == '&':
return '&'
return ch
def code(ch):
if ch == '\n':
return 10
if ch == '\r':
return 13
if ch == '\t':
return 9
chars = ' !"#' + "%&'()*+,-./0" + '123456789:;' + '<' + '=' + '>' + '?@' + 'ABCDEFGHIJKLMNOP' + 'QRSTUVWXYZ[\\]^_`' + 'abcdefghijklmnop' + 'qrstuvwxyz{|}~'
i = 0
n = len(chars)
while i < n:
if chars[i] == ch:
if i < 4:
return 32 + i
return 33 + i
i = i + 1
return -1
def solve(s):
vals = []
for ch in s:
vals = vals + [code(tweak(ch))]
alpha = 'ABCDEFGHIJKLMNOP' + 'QRSTUVWXYZ' + 'abcdefghijklmnop' + 'qrstuvwxyz' + '0123456789' + '-_'
out = ''
i = 0
n = len(vals)
while i < n:
b0 = vals[i]
b1 = 0
b2 = 0
have = 1
if i + 1 < n:
b1 = vals[i + 1]
have = 2
if i + 2 < n:
b2 = vals[i + 2]
have = 3
c0 = b0 // 4
c1 = (b0 % 4) * 16 + (b1 // 16)
c2 = (b1 % 16) * 4 + (b2 // 64)
c3 = b2 % 64
out = out + alpha[c0] + alpha[c1]
if have == 1:
out = out + '='
out = out + '='
else:
if have == 2:
out = out + alpha[c2]
out = out + '='
else:
out = out + alpha[c2] + alpha[c3]
i = i + 3
return out
12 test cases ship with it; the first three:
| in | out |
|---|---|
xmj kospal xmj bmz 0 | eG1qIGtvc3BhbAp4bWogIGJteiAw |
wogf dqlj wogf ityul 1 | d29nZiBkcWxqCndvZ2YgIGl0eXVsIDE= |
ydrps kwv ydrps nah 2 | eWRycHMga3d2CnlkcnBzICBuYWggMg== |
GET https://research.zaiba.cloud/api/v1/programs/base64url_encode
POST https://research.zaiba.cloud/api/v1/programs/base64url_encode/run
{"input": "…"}
# 200 · X-DeAI-Rail: grant (free within the allowance) · output + signed receipt
GET https://research.zaiba.cloud/api/v1/programs/base64url_encode/code
# 200 · text/plain · the tool as one Python file, written by the library's authors