-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path001b_spreadsheets.py
45 lines (38 loc) · 1.1 KB
/
001b_spreadsheets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#! /usr/bin/python
import sys
import re
def num2col(num):
num -= 1
col = []
while True:
remain = num % 26
col.append(chr(ord('A') + remain))
num = (num - remain) / 26 - 1
if num < 0:
break
return ''.join(reversed(col))
def col2num(col):
num = 0
for c in col:
num = num * 26 + ord(c) - ord('A') + 1
return num
def solve(data):
m = re.search('R(\d+)C(\d+)', data)
if m is not None:
return num2col(int(m.group(2))) + m.group(1)
else:
m = re.search('([A-Z]+)(\d+)', data)
return 'R' + m.group(2) + 'C' + str(col2num(m.group(1)))
def test():
import random
for i in range(10000):
if 0 == random.randint(0, 1):
input = 'R%dC%d' % (random.randint(1, 10**5), random.randint(1, 10**5))
else:
input = '%s%d' % (num2col(random.randint(1, 10**5)), random.randint(1, 10**5))
print 'input = ' + input
print 'output = ' + solve(input)
if __name__ == '__main__':
sys.stdin.readline()
for line in sys.stdin.readlines():
print solve(line)