Skip to content

Commit

Permalink
Implement reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
dantp-ai committed Mar 5, 2024
1 parent 7ba29fd commit 2037d13
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
16 changes: 10 additions & 6 deletions minitorch/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 1 addition & 2 deletions tests/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2037d13

Please sign in to comment.