Created
January 29, 2014 14:43
-
-
Save apalala/8689371 to your computer and use it in GitHub Desktop.
Convert the given integer to the base and symbols of the given 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
import string | |
ALPHABET = string.digits + string.ascii_letters | |
def rebase(value, width=1, alphabet=ALPHABET): | |
""" Convert the given integer to the base | |
and symbols of the given alphabet | |
""" | |
n = len(alphabet) | |
result = [] | |
while value != 0: | |
c = value % n | |
result.append(alphabet[c]) | |
value //= n | |
np = width - len(result) | |
if np <= 0: | |
padding = [] | |
else: | |
padding = [alphabet[0]] * np | |
sresult = ''.join(padding + result) | |
return sresult | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment