from/toAZ starts at 1
This commit is contained in:
parent
e0a878464a
commit
ed32023686
1 changed files with 12 additions and 8 deletions
20
ox/format.py
20
ox/format.py
|
@ -9,18 +9,20 @@ def toAZ(num):
|
||||||
"""
|
"""
|
||||||
Converts an integer to bijective base 26 string using A-Z
|
Converts an integer to bijective base 26 string using A-Z
|
||||||
|
|
||||||
>>> for i in range(0, 1000): assert fromAZ(toAZ(i)) == i
|
>>> for i in range(1, 1000): assert fromAZ(toAZ(i)) == i
|
||||||
|
|
||||||
>>> toAZ(0)
|
>>> toAZ(1)
|
||||||
'A'
|
'A'
|
||||||
|
|
||||||
|
>>> toAZ(4461)
|
||||||
|
'FOO'
|
||||||
|
|
||||||
>>> toAZ(1234567890)
|
>>> toAZ(1234567890)
|
||||||
'CYWOQVK'
|
'CYWOQVK'
|
||||||
"""
|
"""
|
||||||
if num < 0: raise ValueError, "must supply a positive integer"
|
if num < 1: raise ValueError, "must supply a positive integer"
|
||||||
digits = string.letters[26:]
|
digits = string.letters[26:]
|
||||||
az = ''
|
az = ''
|
||||||
num += 1
|
|
||||||
while num != 0:
|
while num != 0:
|
||||||
num, r = divmod(num, 26)
|
num, r = divmod(num, 26)
|
||||||
u, r = divmod(r - 1, 26)
|
u, r = divmod(r - 1, 26)
|
||||||
|
@ -33,18 +35,20 @@ def fromAZ(num):
|
||||||
Converts a bijective base 26 string to an integer
|
Converts a bijective base 26 string to an integer
|
||||||
|
|
||||||
>>> fromAZ('A')
|
>>> fromAZ('A')
|
||||||
0
|
1
|
||||||
>>> fromAZ('AA')
|
>>> fromAZ('AA')
|
||||||
26
|
27
|
||||||
>>> fromAZ('AAA')
|
>>> fromAZ('AAA')
|
||||||
702
|
703
|
||||||
|
>>> fromAZ('FOO')
|
||||||
|
4461
|
||||||
"""
|
"""
|
||||||
num = num.replace('-','')
|
num = num.replace('-','')
|
||||||
digits = string.letters[26:]
|
digits = string.letters[26:]
|
||||||
r = 0
|
r = 0
|
||||||
for exp, char in enumerate(reversed(num)):
|
for exp, char in enumerate(reversed(num)):
|
||||||
r = r + (pow(26, exp) * (digits.index(char) + 1))
|
r = r + (pow(26, exp) * (digits.index(char) + 1))
|
||||||
return r - 1
|
return r
|
||||||
|
|
||||||
def to26(q):
|
def to26(q):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue