File: hexspeak/solution.py

Date: 2026-06-06

Time: 17:00

hexspeak/solution.py

Purpose

This file implements LeetCode 1271 - Hexspeak. It converts a decimal number (given as a string) into a "Hexspeak" representation where hex digits 0 and 1 are replaced with letters O and I, and the result is valid only if every character is one of {A, B, C, D, E, F, I, O}. The file owns both the solution and its unit tests — a self-contained module following the repo's per-problem convention.

Key Components

to_hexspeak(num: str) -> str — The sole function. Takes a decimal integer as a string, returns either a valid Hexspeak string or "ERROR".

The contract:

TestToHexspeak — 11 test cases covering the examples from the problem, edge cases (single digits, large values, all-letter hex like DEADBEEF), and invalid-digit rejection.

Patterns

Three-step pipeline idiom: The function is a clean transform chain — convert to hex, substitute characters, validate. No branching in the transform phase; the only conditional is the final validity gate. This is idiomatic for problems where the core logic is "transform then validate."

Inline tests: The unittest class lives in the same file as the solution, following the repo-wide convention of solution.py containing both implementation and tests (though some problems split tests into test_solution.py).

Dependencies

Imports: Only unittest from the standard library. No external dependencies. The hex conversion uses Python's built-in hex().

Imported by: The massive importedby list is misleading — those are test files across the entire repo. They likely share a common test runner infrastructure, not a direct import of tohexspeak. The function itself has no downstream consumers.

Flow


"257" → int("257") = 257
      → hex(257) = "0x101"
      → [2:] = "101"
      → .upper() = "101"
      → .replace("0","O").replace("1","I") = "IOI"
      → all chars in {A,B,C,D,E,F,I,O}? → yes → "IOI"

The .upper() call is necessary because hex() returns lowercase hex letters (a-f), but Hexspeak requires uppercase.

The two .replace() calls are order-independent here — "0" and "1" don't overlap with each other or with "O"/"I" in the input (hex output only contains 0-9a-f).

Invariants

Error Handling

The only "error" path is returning the string "ERROR" — this is the problem's specified output, not an exception. No exceptions are caught or raised. Invalid input (non-numeric string) would propagate a ValueError from int(num) uncaught, which is acceptable since LeetCode guarantees valid input.

Topics to Explore

Beliefs