File: excel-sheet-column-number/solution.py

Date: 2026-06-06

Time: 16:29

excel-sheet-column-number/solution.py

Purpose

This file solves LeetCode #171 — Excel Sheet Column Number. It converts an Excel-style column label (e.g., "A", "Z", "AB") into its 1-indexed column number (1, 26, 28). It's the inverse of the sibling problem in excel-sheet-column-title/.

Key Components

titletonumber(columnTitle: str) -> int — The sole function. Takes an uppercase letter string and returns the corresponding column number. Contract: input is a non-empty string of uppercase ASCII letters; output is a positive integer.

Patterns

The function treats the column title as a base-26 number with digits A=1 through Z=26 (not 0-25 — there's no zero digit). It uses Horner's method to evaluate the polynomial: process characters left-to-right, multiplying the running total by 26 and adding the current digit's value.

For "AB": 0*26 + 1 = 1, then 1*26 + 2 = 28.

This is the standard idiom for positional numeral conversion — identical in structure to int(s, base) but offset by 1 because Excel columns are 1-indexed per position.

Dependencies

Imports: None. Uses only the built-in ord().

Imported by: The "Imported By" list in the prompt is misleading — those are test files across the entire repo that share a common test harness import pattern, not files that actually call titletonumber. The real direct consumer is excel-sheet-column-number/test_solution.py.

Flow

1. Initialize result = 0.

2. For each character c in columnTitle (left to right):

3. Return the accumulated integer.

The loop body is a single expression — no branching, no early exit.

Invariants

Error Handling

None. The function trusts its caller completely — consistent with LeetCode solution conventions where input constraints are guaranteed by the problem.

Topics to Explore

Beliefs