Created
January 18, 2025 03:46
-
-
Save benatkin/44203c7e09230193c5abd6d4643aaed3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def encode(s): | |
""" | |
Encode text containing lowercase letters and spaces as a base32 integer. | |
""" | |
return sum([ | |
(ord('{' if c == ' ' else c) - ord('a') + 1) << 5 * i | |
for i, c in enumerate(s) | |
]) | |
def decode(n): | |
""" | |
Decode text represented as an integer with encode() | |
""" | |
return ''.join( | |
chr(((n >> 5 * i) & 31) + ord('a') - 1) | |
for i in it.takewhile(lambda i: n >> 5 * i, count(0)) | |
).replace('{', ' ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment