Skip to content

Instantly share code, notes, and snippets.

@benatkin
Created January 18, 2025 03:46
Show Gist options
  • Save benatkin/44203c7e09230193c5abd6d4643aaed3 to your computer and use it in GitHub Desktop.
Save benatkin/44203c7e09230193c5abd6d4643aaed3 to your computer and use it in GitHub Desktop.
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