Skip to content

Instantly share code, notes, and snippets.

@viveksyngh
Created August 5, 2015 19:22
Show Gist options
  • Save viveksyngh/dc7118d8a554e903ef7a to your computer and use it in GitHub Desktop.
Save viveksyngh/dc7118d8a554e903ef7a to your computer and use it in GitHub Desktop.
Given a column title as appears in an Excel sheet, return its corresponding column number.
def titleToNumber(self, A):
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
num = 0
for i in range(len(A)) :
num += (alphabet.index(A[i]) + 1) * (26 ** (len(A) - i - 1) )
return num
#A -> 1
#B -> 2
#C -> 3
#...
#Z -> 26
#AA -> 27
#AB -> 28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment