Skip to content

Instantly share code, notes, and snippets.

@technillogue
Created June 28, 2022 05:24
Show Gist options
  • Select an option

  • Save technillogue/94212903213481f1dfbaa6f63543d582 to your computer and use it in GitHub Desktop.

Select an option

Save technillogue/94212903213481f1dfbaa6f63543d582 to your computer and use it in GitHub Desktop.
import unicodedata
def unicode_character_name(i: int) -> str:
"""Tries to get the unicode name for a given character ordinal.
Useful for finding various quotation marks"""
try:
return unicodedata.name(chr(i))
except ValueError:
return ""
spaces = {
unicode_character_name(i): chr(i)
for i in range(0, 0x10FFF)
if "space" in (name := unicode_character_name(i).lower())
and not any((bad in name for bad in ("wordspace", "mark", "symbol", "fill")))
}
alphabet = "".join(spaces.values())
def number_to_base(n, b):
if n == 0:
return [0]
digits = []
while n:
digits.append(int(n % b))
n //= b
return digits[::-1]
def encode(n):
return "".join(alphabet[index] for index in number_to_base(n, len(alphabet)))
def decode(encoded):
return sum(
[
len(alphabet) ** position * alphabet.index(char)
for position, char in enumerate(reversed(encoded))
]
)
assert decode(encode(28657))
print("spam" + encode(28657) + "bar")
#spam  bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment