Date: 2026-06-06
Time: 15:19
This file solves LeetCode 2455: Average Value of Even Numbers That Are Divisible by Three. It owns exactly one responsibility: given a list of positive integers, compute the floor-average of elements that are both even and divisible by 3, returning 0 if no such elements exist.
averageevendivisiblebythree(nums: list[int]) -> intThe sole exported function. Contract:
The n % 6 == 0 collapse. The problem title says "even numbers divisible by three," which means n % 2 == 0 and n % 3 == 0. The solution collapses both checks into n % 6 == 0 — since lcm(2, 3) = 6, a number is divisible by both 2 and 3 if and only if it's divisible by 6. This is a standard number-theory simplification seen across many solutions in this repo.
Single-pass accumulation. Instead of filtering into a list and then computing the average (which would allocate O(k) memory for qualifying elements), the code accumulates total and count in a single pass. This is O(1) auxiliary space, O(n) time.
Integer division via //. The problem specifies rounding down, which Python's // operator handles correctly for non-negative values.
test_solution.py in the same directory, plus the massive list of test files shown in the "Imported By" section. That import list is an artifact of the test harness structure, not actual cross-problem coupling — each test file likely imports a shared test runner or conftest that transitively references all solutions.1. Initialize total and count to 0.
2. Iterate over every element n in nums.
3. If n % 6 == 0, add n to total and increment count.
4. After the loop, return total // count if count > 0, else 0.
No early exits, no branching beyond the single modulo check.
count tracks exactly how many multiples of 6 have been seen. It's never negative and never exceeds len(nums).total is the sum of all multiples of 6 in nums. Since all inputs are positive integers, total >= 0 always holds.if count ternary. The function never raises ZeroDivisionError.There is none — and intentionally so. This is a LeetCode solution operating under the problem's guarantees (1 <= nums.length <= 1000, 1 <= nums[i] <= 1000). The function trusts its caller to provide a non-empty list of positive integers. Empty lists produce 0 (the fallback), but no explicit validation or exception raising exists.
average-value-of-even-numbers-that-are-divisible-by-three/test_solution.py — See what edge cases the test suite covers (empty qualifying set, all-qualifying, single element)average-value-of-even-numbers-that-are-divisible-by-three/review.md — Check if the review noted the % 6 optimization or flagged alternativeslcm-modular-collapse — The % 6 idiom recurs across problems involving coprime divisibility checks; worth recognizing as a patternmean-of-array-after-removing-some-elements/solution.py — Another averaging problem that likely uses similar accumulation but with different filtering logicaverage-salary-excluding-the-minimum-and-maximum-salary/solution.py — Contrast how a different averaging problem handles exclusion criteriamod-6-equivalence — n % 6 == 0 is equivalent to n % 2 == 0 and n % 3 == 0 for all integers, making the single-check optimization correctzero-on-empty-qualifying-set — The function returns 0 (not an error) when no elements in nums are divisible by 6single-pass-o1-space — The function uses O(1) auxiliary space regardless of input size, accumulating sum and count without allocating a filtered listfloor-division-semantics — Python's // operator provides the floor division required by the problem spec, which is correct here because total and count are both non-negative