Created
November 6, 2011 14:02
-
-
Save kosugi/1342916 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| # http://d.hatena.ne.jp/JunichiIto/20111102/1320253815 | |
| A = ord('A') | |
| N = ord('Z') - A + 1 | |
| def to_n(expr): | |
| return reduce(lambda n, c: n * N + ord(c) - A + 1, expr, 0) | |
| def to_aa(n): | |
| s = [] | |
| while 0 < n: | |
| m = (n - 1) % N | |
| s.append(chr(A + m)) | |
| n = (n - 1) // N | |
| return ''.join(reversed(s)) | |
| def do(): | |
| try: | |
| import re | |
| import sys | |
| mode = int(sys.argv[1]) | |
| if mode == 0: | |
| expr = sys.argv[2].upper() | |
| if re.match(r'^[A-Z]+$', expr): | |
| print to_n(expr) | |
| return | |
| elif mode == 1: | |
| n = int(sys.argv[2]) | |
| if 0 < n: | |
| print to_aa(n) | |
| return | |
| except: | |
| pass | |
| print 'usage: %s mode expr' % sys.argv[0] | |
| if __name__ == '__main__': | |
| do() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment