Skip to content

Instantly share code, notes, and snippets.

@pixelchai
Last active May 24, 2021 00:45
Show Gist options
  • Save pixelchai/b7b78162f009b40e585816acb8aa0d34 to your computer and use it in GitHub Desktop.
Save pixelchai/b7b78162f009b40e585816acb8aa0d34 to your computer and use it in GitHub Desktop.
Encode an int into a compact string representation using an arbitrary alphabet
# 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