Date: 2026-06-06
Time: 18:18
number-of-even-and-odd-bits/solution.pyThis file solves LeetCode 2595: Number of Even and Odd Bits. It's one solution module in a large collection (~500+) of LeetCode implementations, each following the same directory convention: {problem-slug}/solution.py.
The single function evenoddindices classifies the set bits (1-bits) in the binary representation of n by whether they sit at even-indexed or odd-indexed positions (0-indexed from the least significant bit).
evenoddindices(n: int) -> list[int] — The sole public function. Contract:
n in [1, 1000].[evencount, oddcount] where evencount is the number of 1-bits at even bit positions (0, 2, 4, ...) and oddcount at odd positions (1, 3, 5, ...).Bit-walking loop: Rather than converting to a binary string, the code walks the bits from LSB to MSB using n & 1 (extract lowest bit) and n >>= 1 (shift right). A separate counter i tracks the current bit index. This is the idiomatic low-level approach — no string allocation, O(log n) iterations.
Dual accumulator: even and odd are accumulated in a single pass, branching on i % 2. The final return packs them into a two-element list matching LeetCode's expected output format.
Imports: None. The solution is self-contained with no standard library or third-party dependencies.
Imported by: The massive "Imported By" list in the prompt is misleading — those are test files from *other* problems. The actual consumer is number-of-even-and-odd-bits/test_solution.py, which imports this function to run test cases. The other listed files likely share a common test harness pattern that imports from a relative solution module, not from this specific file.
1. Initialize even = odd = 0 and bit-position counter i = 0.
2. While n is nonzero (has remaining bits):
n & 1).even or odd based on whether i is even or odd.n by 1, increment i.3. Return [even, odd].
For n = 50 (binary 110010): bit 1 at index 1 (odd), bit 4 at index 4 (even), bit 5 at index 5 (odd) → [1, 2].
i always equals the number of bits already processed, so it correctly identifies even/odd positions.n >>= 1 on a positive integer eventually reaches 0.n >= 1 per the problem constraints — for n = 0, it returns [0, 0] (the loop body never executes), which is correct but outside the stated domain.None. The function trusts its caller to provide a valid positive integer. No validation, no exceptions. This is appropriate for a LeetCode solution where inputs are guaranteed by the judge.
number-of-even-and-odd-bits/test_solution.py — See how the function is tested and what edge cases are coveredcounting-bits/solution.py:countBits — Related bit-manipulation problem that counts set bits across a rangenumber-of-1-bits/solution.py:hammingWeight — The classic popcount problem; compare bit-walking techniquesbit-index-conventions — Whether "even index" means position 0,2,4 from LSB or MSB matters; verify against LeetCode's definitionnumber-of-even-and-odd-bits/plan.md — The planning document may capture alternative approaches considered (e.g., bin() string conversion)even-odd-bits-zero-indexed-from-lsb — Bit index 0 is the least significant bit; the loop processes bits LSB-first via right-shifteven-odd-bits-single-pass — The function classifies all set bits in exactly one pass over the binary representation, with no second traversal or string conversioneven-odd-bits-no-dependencies — The solution imports nothing; it uses only built-in integer operations (&, >>=, %)even-odd-bits-terminates-on-zero — The while-loop guard while n guarantees termination because n >>= 1 strictly decreases a positive integer toward 0