Created
February 6, 2023 21:17
-
-
Save les-peters/5b18c407ee8b8f88a944b0710dfdfe5f to your computer and use it in GitHub Desktop.
Excel columns
This file contains 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
question = """ | |
Spreadsheet programs often use the A1 Reference Style to refer | |
to columns. Given a column name in this style, return its column number. | |
Eexcel_columnamples of column names to their numbers: | |
A -> 1 | |
B -> 2 | |
C -> 3 | |
// etc | |
Z -> 26 | |
AA -> 27 | |
AB -> 28 | |
// etc | |
AAA -> 703 | |
""" | |
def excel_column(str): | |
o = 0 | |
i = 0 | |
for ch in str: | |
o = o + (ord(ch) - 64) | |
i += 1 | |
if i < len(str): | |
o = o * 26 | |
print(o) | |
excel_column("A") | |
excel_column("B") | |
excel_column("C") | |
excel_column("Z") | |
excel_column("AA") | |
excel_column("AB") | |
excel_column("AAA") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment