Created
January 20, 2013 00:40
-
-
Save ianjosephwilson/4576039 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
import string | |
def to_index(col): | |
col = col.lower() | |
assert len(col) > 0 and not \ | |
[l for l in col if l not in string.lowercase] | |
index = 0 | |
for i in range(1, len(col) + 1): | |
if i == 1: | |
multiplier = 1 | |
else: | |
multiplier = (i-1) * 26 | |
index += (string.lowercase.index(col[-i]) + 1) * multiplier | |
return index | |
assert to_index('A') == 1 | |
assert to_index('AA') == 27 | |
assert to_index('Z') == 26 | |
assert to_index('AB') == 28 | |
try: | |
to_index('3') | |
except AssertionError: | |
pass | |
else: | |
assert False, 'col 3 is undefined' | |
try: | |
to_index('') | |
except AssertionError: | |
pass | |
else: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment