Created
June 28, 2022 05:24
-
-
Save technillogue/94212903213481f1dfbaa6f63543d582 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
| 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