Created
November 11, 2014 11:49
-
-
Save toshsan/ded6afc44fd916e2ccb8 to your computer and use it in GitHub Desktop.
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
BASE_ALPH = tuple("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") | |
BASE_DICT = dict((c, v) for v, c in enumerate(BASE_ALPH)) | |
BASE_LEN = len(BASE_ALPH) | |
def base_decode(string): | |
num = 0 | |
for char in string: | |
num = num * BASE_LEN + BASE_DICT[char] | |
return num | |
def base_encode(num): | |
if not num: | |
return BASE_ALPH[0] | |
encoding = "" | |
while num: | |
num, rem = divmod(num, BASE_LEN) | |
encoding = BASE_ALPH[rem] + encoding | |
return encoding |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment