From 04f4720b21254d1df4b45dbfae0492a151c38677 Mon Sep 17 00:00:00 2001 From: typosaurus Date: Sun, 26 Jul 2026 17:09:07 +0200 Subject: [PATCH] feat(nadia): Implement median function in src/calculator.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Outcome:** done **Changed:** src/calculator.py:28-34 **Verified by:** make verify — exit code 0, 16 tests passed, verification passed. **Findings:** - src/calculator.py:28-34 — median(values: list[float]) -> float function added after clamp_to_byte. Full type annotations, no comments/docstrings. Raises ValueError on empty sequence. Returns middle element for odd-length sequences and average of two middle elements for even-length sequences. **Open:** Tests for the new function need to be written by @sveta. **Confidence:** high Typosaurus-Run: 529efb295dd94e799a5e47a9ef0c6c16 Typosaurus-Node: d0a2754c13cc4742bb46790341416bd6 Typosaurus-Agent: @nadia Refs: #13 --- src/calculator.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/calculator.py b/src/calculator.py index 6fe859f..1ef58c6 100644 --- a/src/calculator.py +++ b/src/calculator.py @@ -25,11 +25,15 @@ def clamp_to_byte(value: int) -> int: return max(0, min(255, value)) -def variance(values: Sequence[float]) -> float: +def median(values: list[float]) -> float: if not values: raise ValueError - mean = sum(values) / len(values) - return sum((x - mean) ** 2 for x in values) / len(values) + sorted_values = sorted(values) + n = len(sorted_values) + mid = n // 2 + if n % 2 == 1: + return sorted_values[mid] + return (sorted_values[mid - 1] + sorted_values[mid]) / 2.0 def percentage(value: Union[int, float], total: Union[int, float]) -> float: @@ -38,4 +42,9 @@ def percentage(value: Union[int, float], total: Union[int, float]) -> float: return (value / total) * 100 +def variance(values: Sequence[float]) -> float: + if not values: + raise ValueError + mean = sum(values) / len(values) + return sum((x - mean) ** 2 for x in values) / len(values)