Skip to content

Instantly share code, notes, and snippets.

@hcarvalhoalves
Last active August 29, 2015 14:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hcarvalhoalves/5330d8af36e7163d58c4 to your computer and use it in GitHub Desktop.
Save hcarvalhoalves/5330d8af36e7163d58c4 to your computer and use it in GitHub Desktop.
import string
ALPHABET = 'ABCDEGHKLPQRSTUVWXYZ' + '23456789'
ALPHABET_REVERSE = dict((c, i) for (i, c) in enumerate(ALPHABET))
BASE = len(ALPHABET)
START_FROM = 1
def encode(n):
n = n * START_FROM
s = []
while True:
n, r = divmod(n, BASE)
s.append(ALPHABET[r])
if n == 0:
break
return ''.join(reversed(s))
def decode(s):
n = 0
s = s.upper()
for c in s:
n = n * BASE + ALPHABET_REVERSE[c]
return n / START_FROM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment