El reto
Cree una función que convierta el valor de String hacia y desde Base64 utilizando el juego de caracteres ASCII.
No utilice funciones integradas.
Ejemplos:
# ought to return 'dGhpcyBpcyBhIHN0cmluZyEh'
to_base_64('it is a string!!')
# ought to return 'it is a string!!'
from_base_64('dGhpcyBpcyBhIHN0cmluZyEh')
Puede obtener más información sobre la codificación y decodificación Base64 aquí.
La solución en código Python
Opción 1:
CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
def to_base_64(string):
padding = 3 - len(string) % 3 if len(string) % 3 else 0
binary = ''.be part of(format(ord(i),'08b') for i in string) + '00'*padding
return ''.be part of(CODES(int(binary(i:i+6), 2)) for i in vary(0, len(binary), 6))
def from_base_64(string):
binary = ''.be part of(format(CODES.discover(i),'06b') for i in string)
return ''.be part of(chr(int(binary(i:i+8), 2)) for i in vary(0, len(binary), 8)).rstrip('x00')
Opcion 2:
import string
base = string.ascii_uppercase+string.ascii_lowercase+string.digits+"+/"
def to_base_64(strng):
ls = "".be part of((bin(ord(i))(2:).zfill(8) for i in strng))
ls2 = (int(ls(i:i+6).ljust(6, '0'),2) for i in vary(0,len(ls),6))
return "".be part of((base(ipercent64) for i in ls2)).substitute("x00","")
def from_base_64(strng):
ls = "".be part of((bin(base.index(i))(2:).zfill(6) for i in strng))
ls2 = (int(ls(i:i+8),2) for i in vary(0,len(ls),8))
return "".be part of((chr(i) for i in ls2)).substitute("x00","")
Opción 3:
caps = (chr(c + 65) for c in vary(26))
lows = (chr(c + 97) for c in vary(26))
nums = (str(n) for n in vary(10))
desk = caps + lows + nums + ('+', '-')
num24 = lambda n3: sum( ((n3(i) << (16 - (8 * i)) ) for i in vary(len(n3))) )
code = lambda n3: ( (num24(n3) >> (18 - (6 * i))) % 64 for i in vary(len(n3) + 1) )
t = lambda s3: (ord(c) for c in s3)
to64 = lambda s3: ''.be part of( (desk(c) for c in code(t(s3)) ) )
num6 = lambda n4: sum( ( (n4(i) << (18 - (6 * i) )) for i in vary(len(n4)) ) )
decode = lambda n4: ( (num6(n4) >> (16 - (8 * i) )) % 256 for i in vary(len(n4) - 1) )
d = lambda s4: (desk.index(c) for c in s4)
from64 = lambda s4: ''.be part of( chr(c) for c in decode(d(s4)) )
def to_base_64(string):
return ''.be part of( map(to64, (string(i:i+3) for i in vary(0, len(string), 3))) )
def from_base_64(string):
return ''.be part of( map(from64, (string(i:i+4) for i in vary(0, len(string), 4))) )
Casos de prueba para validar nuestra solución
from answer import to_base_64, from_base_64
import take a look at
@take a look at.describe("Fundamental assessments")
def _():
@take a look at.it("Checks")
def __():
instances = (("it is a string!!","dGhpcyBpcyBhIHN0cmluZyEh"),
("it is a take a look at!","dGhpcyBpcyBhIHRlc3Qh"),
("now's the time for all good males to come back to assistance from their nation.","bm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBtZW4gdG8gY29tZSB0byB0aGUgYWlkIG9mIHRoZWlyIGNvdW50cnku"),
("1234567890 ", "MTIzNDU2Nzg5MCAg"),
("ABCDEFGHIJKLMNOPQRSTUVWXYZ ", "QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVog"),
("the fast brown fox jumps over the white fence. ","dGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSB3aGl0ZSBmZW5jZS4g"),
("dGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSB3aGl0ZSBmZW5jZS4","ZEdobElIRjFhV05ySUdKeWIzZHVJR1p2ZUNCcWRXMXdjeUJ2ZG1WeUlIUm9aU0IzYUdsMFpTQm1aVzVqWlM0"),
("VFZSSmVrNUVWVEpPZW1jMVRVTkJaeUFna","VkZaU1NtVnJOVVZXVkVwUFpXMWpNVlJWVGtKYWVVRm5h"),
("TVRJek5EVTJOemc1TUNBZyAg","VFZSSmVrNUVWVEpPZW1jMVRVTkJaeUFn"))
for x, y in instances:
outcome=to_base_64(x)
take a look at.assert_equals(outcome, y)
take a look at.assert_equals(from_base_64(outcome), x)