Date: 2026-06-06
Time: 16:58
hamming-distance/solution.pyThis file solves LeetCode #461 — Hamming Distance. It owns exactly one responsibility: counting the number of bit positions where two integers differ. This is the canonical definition of Hamming distance in information theory.
hammingDistance(x: int, y: int) -> int — The sole function. Takes two non-negative integers bounded by [0, 2^31 - 1] and returns the count of differing bits.
The implementation is a single expression with two operations chained together:
1. x ^ y — XOR produces a number where each 1 bit marks a position where x and y differ.
2. bin(...).count('1') — Converts to a binary string (e.g., '0b1010') and counts the '1' characters.
This follows the bit manipulation via string conversion idiom — a Pythonic shortcut that trades theoretical elegance for readability. The alternative would be Brian Kernighan's algorithm (n &= n - 1 in a loop), which avoids the string allocation but is harder to read and offers no practical speedup for 32-bit integers.
The solution also follows the repo-wide convention: a single module-level function matching the LeetCode method signature, with type hints and a docstring.
Imports: None. Pure Python builtins only (bin, str.count, ^ operator).
Imported by: The hamming-distance/test_solution.py file. The massive "Imported By" list in the prompt is misleading — those are unrelated test files across the repo that happen to share a test harness or import pattern, not actual consumers of hammingDistance.
Entirely linear, no branching:
x, y → XOR → int with differing bits set → bin() → "0b..." string → count('1') → return
For hammingDistance(1, 4): 1 ^ 4 = 5 → bin(5) = '0b101' → '0b101'.count('1') = 2 → returns 2.
0 <= x, y <= 2^31 - 1 is documented but not enforced — no validation. Negative inputs would still produce a result in Python (since bin(-1) yields '-0b1'), but the 1 count would be wrong because Python integers have arbitrary precision and negative XOR results use two's complement conceptually but bin() doesn't emit all the leading 1s.[0, 31] for valid inputs (at most 31 bits can differ).None. No input validation, no try/except. Invalid inputs (negative numbers, floats, None) would either produce wrong results silently or raise a TypeError from the ^ operator.