MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / excel_title_to_column

Function excel_title_to_column

conversions/excel_title_to_column.py:1–27  ·  view source on GitHub ↗

Given a string column_title that represents the column title in an Excel sheet, return its corresponding column number. >>> excel_title_to_column("A") 1 >>> excel_title_to_column("B") 2 >>> excel_title_to_column("AB") 28 >>> excel_title_to_column("Z") 26

(column_title: str)

Source from the content-addressed store, hash-verified

1def 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
30if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected