Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet. [#168]
For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ... Examples: Input: 1 Output: "A" Input: 28 Output: "AB" Input: 701 Output: "ZY"
>>> def num2chr(n): ret = '' while n//26: n,m = divmod(n-1,26) ret = chr(m+65) + ret if n:ret = chr(n+64) + ret return ret >>> num2chr(1) 'A' >>> num2chr(28) 'AB' >>> num2chr(701) 'ZY' >>> num2chr(703) 'AAA' >>> num2chr(1502) 'BET' >>> num2chr(16384) 'XFD' >>> num2chr(17860) 'ZJX' >>>
Excel Sheet Column Number
Given a column title as appear in an Excel sheet, return its corresponding column number. [#171]
For example:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ... Examples: Input: "A" Output: 1 Input: "AB" Output: 28 Input: "ZY" Output: 701
>>> def chr2num(s): return sum([(ord(n)-64)*26**i for i,n in enumerate(list(s)[::-1])]) >>> chr2num('A') 1 >>> chr2num('AB') 28 >>> chr2num('ZY') 701 >>> chr2num('AAA') 703 >>> chr2num('XFD') 16384 >>> >>> chr2num('YRH') 17376