Created
February 20, 2013 20:13
-
-
Save maebert/4999112 to your computer and use it in GitHub Desktop.
Gets the shortest unique code (e.g. for URL shorteners) that avoids confusion between characters (such as I and 1, 0 and O).
This file contains hidden or 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
def get_code(num, alphabet="23456789ABCDEFGHJKLMNPQRSTUVWXYZ"): | |
base = len(alphabet) | |
result = "" | |
num, rem = divmod(num, base) | |
while num: | |
result += alphabet[rem] | |
num, rem = divmod(num, base) | |
result += alphabet[rem] | |
return result[::-1] | |
rows_in_db = 12345 | |
new_shortcode = get_code(rows_in_db + 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment