diff --git a/minitorch/operators.py b/minitorch/operators.py index 1d2fc94..12f0019 100644 --- a/minitorch/operators.py +++ b/minitorch/operators.py @@ -197,17 +197,21 @@ def reduce( $x_1 \ldots x_n$ and computes the reduction :math:`fn(x_3, fn(x_2, fn(x_1, x_0)))` """ - # TODO: Implement for Task 0.3. - raise NotImplementedError("Need to implement for Task 0.3") + + def new_fn(ls: Iterable[float]) -> float: + res = start + for x in ls: + res = fn(res, x) + return res + + return new_fn def sum(ls: Iterable[float]) -> float: "Sum up a list using `reduce` and `add`." - # TODO: Implement for Task 0.3. - raise NotImplementedError("Need to implement for Task 0.3") + return reduce(add, 0)(ls) def prod(ls: Iterable[float]) -> float: "Product of a list using `reduce` and `mul`." - # TODO: Implement for Task 0.3. - raise NotImplementedError("Need to implement for Task 0.3") + return reduce(mul, 1)(ls) diff --git a/tests/test_operators.py b/tests/test_operators.py index 6451283..e1807fc 100644 --- a/tests/test_operators.py +++ b/tests/test_operators.py @@ -198,8 +198,7 @@ def test_sum_distribute(ls1: List[float], ls2: List[float]) -> None: Write a test that ensures that the sum of `ls1` plus the sum of `ls2` is the same as the sum of each element of `ls1` plus each element of `ls2`. """ - # TODO: Implement for Task 0.3. - raise NotImplementedError("Need to implement for Task 0.3") + assert_close(sum(ls1) + sum(ls2), sum(addLists(ls1, ls2))) @pytest.mark.task0_3