Skip to content

Instantly share code, notes, and snippets.

@hcarvalhoalves
Created August 11, 2014 17:01
Show Gist options
  • Select an option

  • Save hcarvalhoalves/a4b86201fdfe4e471251 to your computer and use it in GitHub Desktop.

Select an option

Save hcarvalhoalves/a4b86201fdfe4e471251 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