mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Split base85.py into functions, Add doctests (#5746)
* Update base16.py * Rename base64_encoding.py to base64.py * Split into functions, Add doctests * Update base16.py
This commit is contained in:
parent
24731b078c
commit
bdd135d403
|
@ -1,30 +1,30 @@
|
||||||
import base64
|
import base64
|
||||||
|
|
||||||
|
|
||||||
def encode_to_b16(inp: str) -> bytes:
|
def base16_encode(inp: str) -> bytes:
|
||||||
"""
|
"""
|
||||||
Encodes a given utf-8 string into base-16.
|
Encodes a given utf-8 string into base-16.
|
||||||
|
|
||||||
>>> encode_to_b16('Hello World!')
|
>>> base16_encode('Hello World!')
|
||||||
b'48656C6C6F20576F726C6421'
|
b'48656C6C6F20576F726C6421'
|
||||||
>>> encode_to_b16('HELLO WORLD!')
|
>>> base16_encode('HELLO WORLD!')
|
||||||
b'48454C4C4F20574F524C4421'
|
b'48454C4C4F20574F524C4421'
|
||||||
>>> encode_to_b16('')
|
>>> base16_encode('')
|
||||||
b''
|
b''
|
||||||
"""
|
"""
|
||||||
# encode the input into a bytes-like object and then encode b16encode that
|
# encode the input into a bytes-like object and then encode b16encode that
|
||||||
return base64.b16encode(inp.encode("utf-8"))
|
return base64.b16encode(inp.encode("utf-8"))
|
||||||
|
|
||||||
|
|
||||||
def decode_from_b16(b16encoded: bytes) -> str:
|
def base16_decode(b16encoded: bytes) -> str:
|
||||||
"""
|
"""
|
||||||
Decodes from base-16 to a utf-8 string.
|
Decodes from base-16 to a utf-8 string.
|
||||||
|
|
||||||
>>> decode_from_b16(b'48656C6C6F20576F726C6421')
|
>>> base16_decode(b'48656C6C6F20576F726C6421')
|
||||||
'Hello World!'
|
'Hello World!'
|
||||||
>>> decode_from_b16(b'48454C4C4F20574F524C4421')
|
>>> base16_decode(b'48454C4C4F20574F524C4421')
|
||||||
'HELLO WORLD!'
|
'HELLO WORLD!'
|
||||||
>>> decode_from_b16(b'')
|
>>> base16_decode(b'')
|
||||||
''
|
''
|
||||||
"""
|
"""
|
||||||
# b16decode the input into bytes and decode that into a human readable string
|
# b16decode the input into bytes and decode that into a human readable string
|
||||||
|
|
|
@ -1,13 +1,33 @@
|
||||||
import base64
|
import base64
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def base85_encode(string: str) -> bytes:
|
||||||
inp = input("->")
|
"""
|
||||||
encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object)
|
>>> base85_encode("")
|
||||||
a85encoded = base64.a85encode(encoded) # a85encoded the encoded string
|
b''
|
||||||
print(a85encoded)
|
>>> base85_encode("12345")
|
||||||
print(base64.a85decode(a85encoded).decode("utf-8")) # decoded it
|
b'0etOA2#'
|
||||||
|
>>> base85_encode("base 85")
|
||||||
|
b'@UX=h+?24'
|
||||||
|
"""
|
||||||
|
# encoded the input to a bytes-like object and then a85encode that
|
||||||
|
return base64.a85encode(string.encode("utf-8"))
|
||||||
|
|
||||||
|
|
||||||
|
def base85_decode(a85encoded: bytes) -> str:
|
||||||
|
"""
|
||||||
|
>>> base85_decode(b"")
|
||||||
|
''
|
||||||
|
>>> base85_decode(b"0etOA2#")
|
||||||
|
'12345'
|
||||||
|
>>> base85_decode(b"@UX=h+?24")
|
||||||
|
'base 85'
|
||||||
|
"""
|
||||||
|
# a85decode the input into bytes and decode that into a human readable string
|
||||||
|
return base64.a85decode(a85encoded).decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user