Skip to content

Instantly share code, notes, and snippets.

@ianjosephwilson
Created January 20, 2013 00:40
Show Gist options
  • Save ianjosephwilson/4576039 to your computer and use it in GitHub Desktop.
Save ianjosephwilson/4576039 to your computer and use it in GitHub Desktop.
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