Skip to content

Instantly share code, notes, and snippets.

@simonwex
Created May 10, 2012 19:10
Show Gist options
  • Save simonwex/2655185 to your computer and use it in GitHub Desktop.
Save simonwex/2655185 to your computer and use it in GitHub Desktop.
Semi-obscure short URL algorithm
def rebase(num, numerals="Zv0w2x4y6z8AaBcCeDgEiFkGmHoIqJsKuL3M7NbOfPjQnRrS1T9UhVpW5XlYdt"):
base = len(numerals)
left_digits = num // base
if left_digits == 0:
return numerals[num % base]
else:
return rebase(left_digits, numerals) + numerals[num % base]
>>> rebase(451)
'yD'
>>> rebase(452)
'yg'
>>> rebase(453)
'yE'
>>> rebase(454)
'yi'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment