blob: 10efd3a1fce56c1d612a1b2d12ed95be456427ef (
plain)
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
|
#!/usr/bin/env python3
# Challenge 060
#
# TASK #1 > Excel Column
# Reviewed by: Ryan Thompson
# Write a script that accepts a number and returns the Excel Column Name it
# represents and vice-versa.
#
# Excel columns start at A and increase lexicographically using the 26 letters
# of the English alphabet, A..Z. After Z, the columns pick up an extra "digit",
# going from AA, AB, etc., which could (in theory) continue to an arbitrary
# number of digits. In practice, Excel sheets are limited to 16,384 columns.
#
# Example
# Input Number: 28
# Output: AB
#
# Input Column Name: AD
# Output: 30
import re
import sys
def col2num(col):
num = 0
for digit in col:
num = 26*num + ord(digit)-ord('A') + 1
return num
def num2col(num):
col = ""
while num > 0:
digit = (num-1) % 26
num = int((num-1) / 26)
col = chr(ord('A')+digit) + col
return col
arg = sys.argv[1]
if re.search(r'^\d+$', arg):
print(num2col(int(arg)))
else:
print(col2num(arg))
|