From ed320236867700d3a7e6875af0d51aff1f099d8e Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sat, 17 Dec 2011 14:02:06 +0530 Subject: [PATCH] from/toAZ starts at 1 --- ox/format.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/ox/format.py b/ox/format.py index 5323ea3..61664ba 100644 --- a/ox/format.py +++ b/ox/format.py @@ -9,18 +9,20 @@ def toAZ(num): """ 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' + >>> toAZ(4461) + 'FOO' + >>> toAZ(1234567890) '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:] az = '' - num += 1 while num != 0: num, r = divmod(num, 26) u, r = divmod(r - 1, 26) @@ -33,18 +35,20 @@ def fromAZ(num): Converts a bijective base 26 string to an integer >>> fromAZ('A') - 0 + 1 >>> fromAZ('AA') - 26 + 27 >>> fromAZ('AAA') - 702 + 703 + >>> fromAZ('FOO') + 4461 """ num = num.replace('-','') digits = string.letters[26:] r = 0 for exp, char in enumerate(reversed(num)): r = r + (pow(26, exp) * (digits.index(char) + 1)) - return r - 1 + return r def to26(q): """