Date: 2026-06-06
Time: 15:15
This file implements a solution for the Armstrong Number problem (LeetCode #1134). An Armstrong number (also called a narcissistic number) is an integer where the sum of each digit raised to the power of the total number of digits equals the original number. For example, 153 is Armstrong because 1^3 + 5^3 + 3^3 = 153.
The file owns exactly one responsibility: determining whether a given integer satisfies the Armstrong property.
is_armstrong(n: int) -> boolThe sole public function. Contract:
nTrue if n is an Armstrong number, False otherwiseThe implementation is a single expression — no intermediate state, no mutation.
String-based digit extraction: Converts n to a string to iterate over digits and determine the digit count (k). This is the idiomatic Python approach — avoids the math.log10 + modulo loop that you'd see in C/Java, and handles edge cases like n=0 naturally (single digit, 0^1 == 0).
Generator expression in sum(): sum(int(d) ** k for d in digits) is lazy — it doesn't materialize a list. For single numbers this doesn't matter, but it's a good habit.
Imports: None. Pure function, no external dependencies.
Imported by: armstrong-number/testsolution.py directly tests this. The massive "Imported By" list in the prompt is misleading — those are unrelated test files that happen to share a common test infrastructure, not actual importers of isarmstrong.
1. Convert n to its string representation → digits
2. Compute k = number of digits (length of the string)
3. For each character d in digits, convert back to int and raise to the k-th power
4. Sum all those powers and compare to the original n
The entire computation is O(k) where k is the number of digits — effectively O(log n).
n is a non-negative integer. Negative inputs would produce digits starting with '-', and int('-') would raise ValueError.k=1, and any digit raised to the 1st power equals itself, so all single-digit numbers are Armstrong numbers by definition.None. The function trusts its caller to pass a valid positive integer, consistent with LeetCode's problem constraints. A negative or non-integer input would propagate a ValueError or TypeError from str() / int().
armstrong-number/test_solution.py — See which edge cases are covered (single digit, large numbers, boundary values)armstrong-number/plan.md — Understand the planning approach used before implementationdigit-decomposition-patterns — Compare string-based vs arithmetic digit extraction across solutions like add-digits, happy-number, and self-dividing-numbershappy-number/solution.py:isHappy — Similar digit-power-sum pattern but with cycle detectionarmstrong-single-digit-always-true — All single-digit non-negative integers (0-9) are Armstrong numbers under this implementation because any digit to the 1st power equals itselfarmstrong-negative-input-raises — Passing a negative integer to is_armstrong raises ValueError because int('-') fails during the generator expressionarmstrong-time-complexity — is_armstrong runs in O(log n) time, iterating once over the digits with constant-time exponentiation per digitarmstrong-no-dependencies — The solution uses only Python builtins (str, len, sum, int, **) with zero imports