Skip to content

Instantly share code, notes, and snippets.

@les-peters
Created February 6, 2023 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save les-peters/5b18c407ee8b8f88a944b0710dfdfe5f to your computer and use it in GitHub Desktop.
Save les-peters/5b18c407ee8b8f88a944b0710dfdfe5f to your computer and use it in GitHub Desktop.
Excel columns
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