Skip to content

Instantly share code, notes, and snippets.

@gorlum0
Created August 27, 2011 11:14
Show Gist options
  • Save gorlum0/1175263 to your computer and use it in GitHub Desktop.
Save gorlum0/1175263 to your computer and use it in GitHub Desktop.
codeforces - 1 - B (py)
#!/usr/bin/env python
"""(c) gorlum0 [at] gmail.com"""
import re
import string
from math import log
from sys import stdin
def int2base(x, base, digits = '0123456789abcdefghijklmnopqrstuvwxyz'):
'''inverse of int(x, base), 2 <= base <= 36'''
if x < 0:
sign = -1
elif x==0:
return '0'
else:
sign = 1
x *= sign
number = []
while x:
number.append(digits[x % base])
x /= base
if sign < 0:
number.append('-')
number.reverse()
return ''.join(number)
def sum_geom(b1, q, n):
return b1 * (q**n - 1) // (q - 1)
def main():
trantab1 = string.maketrans(
(string.digits + string.lowercase)[:26],
string.uppercase)
trantab2 = string.maketrans(
string.uppercase,
(string.digits + string.lowercase)[:26])
t = int(stdin.readline())
for line in stdin:
# exclusive
m1 = re.match(r'\s*R(\d+)C(\d+)\s*$', line)
m2 = re.match(r'\s*([A-Z]+)([0-9]+)\s*$', line)
if m1:
r, c = m1.groups()
c = int(c) - 1
k = int(log(25*c + 1, 26))
c -= sum_geom(1, 26, k) - 1
c = int2base(c, 26)
c = c.translate(trantab1)
print '%s%s' % (c.rjust(k, 'A'), r)
if m2:
c, r = m2.groups()
c = c.translate(trantab2)
c = int(c, 26) + 1 + sum_geom(1, 26, len(c)) - 1
print 'R%sC%d' % (r, c)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment