Last active
May 24, 2021 00:45
-
-
Save pixelchai/b7b78162f009b40e585816acb8aa0d34 to your computer and use it in GitHub Desktop.
Encode an int into a compact string representation using an arbitrary alphabet
This file contains 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
# works on any integer N where N > 0 | |
def int_to_string(n): | |
alphabet = "0123456789abcdefghijklmnopqrstuvwxyz" | |
if n == 0: | |
return "" | |
else: | |
return int_to_string(n // len(alphabet)) + alphabet[n % len(alphabet)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment