Skip to content

Commit a28ad3f

Browse files
banaboipoyea
andauthored
Add Microsoft Excel Column Title to Column Number Conversion (TheAlgorithms#4849)
* Added excel column title to number algorithm as part of conversions * Renamed file to better reflect algorithm function * Removed duplicate file * Update excel_title_to_column.py * Update excel_title_to_column.py Co-authored-by: John Law <[email protected]>
1 parent 5bac76d commit a28ad3f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Diff for: conversions/excel_title_to_column.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def excel_title_to_column(column_title: str) -> int:
2+
"""
3+
Given a string column_title that represents
4+
the column title in an Excel sheet, return
5+
its corresponding column number.
6+
7+
>>> excel_title_to_column("A")
8+
1
9+
>>> excel_title_to_column("B")
10+
2
11+
>>> excel_title_to_column("AB")
12+
28
13+
>>> excel_title_to_column("Z")
14+
26
15+
"""
16+
assert column_title.isupper()
17+
answer = 0
18+
index = len(column_title) - 1
19+
power = 0
20+
21+
while index >= 0:
22+
value = (ord(column_title[index]) - 64) * pow(26, power)
23+
answer += value
24+
power += 1
25+
index -= 1
26+
27+
return answer
28+
29+
30+
if __name__ == "__main__":
31+
from doctest import testmod
32+
33+
testmod()

0 commit comments

Comments
 (0)