Skip to content

Instantly share code, notes, and snippets.

@hmasato
Last active May 16, 2016 15:03
Show Gist options
  • Save hmasato/8979ff25e9d2c5d44883 to your computer and use it in GitHub Desktop.
Save hmasato/8979ff25e9d2c5d44883 to your computer and use it in GitHub Desktop.
[Python] numToAlphaSeq: int value to str sequence
#hmasato
#https://gist.github.com/hmasato/8979ff25e9d2c5d44883
def numToAlphaSeq(intValue, upperCase = True, baseStr = 'abcdefghijklmnopqrstuvwxyz'):
ret = ''
if not str(intValue).isdigit(): return(ret)
baseDecimal = len(baseStr)
while intValue >= 0:
remainder = int(intValue % baseDecimal) #remainder
intValue = (intValue - 1 - remainder) / baseDecimal
ret = baseStr[remainder] + ret
if upperCase: ret = ret.upper()
return(ret)
"""
numToAlphaSeq(0)
# Result: A
numToAlphaSeq(29)
# Result: AD
numToAlphaSeq(1150505010)
# Result: CRUPVLS
numToAlphaSeq(0, upperCase=False)
# Result: a
numToAlphaSeq(29, upperCase=False)
# Result: ad
numToAlphaSeq(1150505010, upperCase=False)
# Result: crupvls
numToAlphaSeq(1150505010, baseStr="ABC")
# Result: BBBACAABABABCCAAACA #
numToAlphaSeq(1150505010, baseStr="012345")
# Result: 154544040520 #
numToAlphaSeq(1150505010, baseStr="0123")
# Result: 233033133223132 #
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment