Skip to content

Commit bdd135d

Browse files
Split base85.py into functions, Add doctests (TheAlgorithms#5746)
* Update base16.py * Rename base64_encoding.py to base64.py * Split into functions, Add doctests * Update base16.py
1 parent 24731b0 commit bdd135d

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

ciphers/base16.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
import base64
22

33

4-
def encode_to_b16(inp: str) -> bytes:
4+
def base16_encode(inp: str) -> bytes:
55
"""
66
Encodes a given utf-8 string into base-16.
77
8-
>>> encode_to_b16('Hello World!')
8+
>>> base16_encode('Hello World!')
99
b'48656C6C6F20576F726C6421'
10-
>>> encode_to_b16('HELLO WORLD!')
10+
>>> base16_encode('HELLO WORLD!')
1111
b'48454C4C4F20574F524C4421'
12-
>>> encode_to_b16('')
12+
>>> base16_encode('')
1313
b''
1414
"""
1515
# encode the input into a bytes-like object and then encode b16encode that
1616
return base64.b16encode(inp.encode("utf-8"))
1717

1818

19-
def decode_from_b16(b16encoded: bytes) -> str:
19+
def base16_decode(b16encoded: bytes) -> str:
2020
"""
2121
Decodes from base-16 to a utf-8 string.
2222
23-
>>> decode_from_b16(b'48656C6C6F20576F726C6421')
23+
>>> base16_decode(b'48656C6C6F20576F726C6421')
2424
'Hello World!'
25-
>>> decode_from_b16(b'48454C4C4F20574F524C4421')
25+
>>> base16_decode(b'48454C4C4F20574F524C4421')
2626
'HELLO WORLD!'
27-
>>> decode_from_b16(b'')
27+
>>> base16_decode(b'')
2828
''
2929
"""
3030
# b16decode the input into bytes and decode that into a human readable string
File renamed without changes.

ciphers/base85.py

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
import base64
22

33

4-
def main() -> None:
5-
inp = input("->")
6-
encoded = inp.encode("utf-8") # encoded the input (we need a bytes like object)
7-
a85encoded = base64.a85encode(encoded) # a85encoded the encoded string
8-
print(a85encoded)
9-
print(base64.a85decode(a85encoded).decode("utf-8")) # decoded it
4+
def base85_encode(string: str) -> bytes:
5+
"""
6+
>>> base85_encode("")
7+
b''
8+
>>> base85_encode("12345")
9+
b'0etOA2#'
10+
>>> base85_encode("base 85")
11+
b'@UX=h+?24'
12+
"""
13+
# encoded the input to a bytes-like object and then a85encode that
14+
return base64.a85encode(string.encode("utf-8"))
15+
16+
17+
def base85_decode(a85encoded: bytes) -> str:
18+
"""
19+
>>> base85_decode(b"")
20+
''
21+
>>> base85_decode(b"0etOA2#")
22+
'12345'
23+
>>> base85_decode(b"@UX=h+?24")
24+
'base 85'
25+
"""
26+
# a85decode the input into bytes and decode that into a human readable string
27+
return base64.a85decode(a85encoded).decode("utf-8")
1028

1129

1230
if __name__ == "__main__":
13-
main()
31+
import doctest
32+
33+
doctest.testmod()

0 commit comments

Comments
 (0)