aboutsummaryrefslogtreecommitdiff
path: root/challenge-060/paulo-custodio/python/ch-1.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-060/paulo-custodio/python/ch-1.py')
-rw-r--r--challenge-060/paulo-custodio/python/ch-1.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-060/paulo-custodio/python/ch-1.py b/challenge-060/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..10efd3a1fc
--- /dev/null
+++ b/challenge-060/paulo-custodio/python/ch-1.py
@@ -0,0 +1,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))