Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions stacked_weighted_probe_20261101.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Dependent layer for the isolated stacked PR review fixture."""

from stacked_probe_20261101 import average


def weighted_average(values, weights=None):
"""Return the weighted mean, or 0 when the total weight is zero."""
if weights is None:
return average(values)
if len(values) != len(weights):
raise ValueError("values and weights must have the same length")
return sum(value * weight for value, weight in zip(values, weights)) / sum(weights)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug · medium
A zero total weight raises ZeroDivisionError instead of returning the promised 0. For example, weighted_average([10], [0]) and weighted_average([], []) both reach this division with a zero denominator. Compute the total weight first and return 0 when it is zero, preserving the existing None fallback and length validation.

Suggestion:

Suggested change
return sum(value * weight for value, weight in zip(values, weights)) / sum(weights)
total_weight = sum(weights)
if total_weight == 0:
return 0
return sum(value * weight for value, weight in zip(values, weights)) / total_weight

Loading