Define the function my_reduce0 such that it emulates reduce. Like the real reduce, it must take a binary function (possibly None), an iterable, and an optional seed. If the iterable is empty and there's no seed, my_reduce0 raises a TypeError exception. If the iterable is empty and there is a seed, my_reduce0 returns the seed. If the iterable is not empty and there's no seed, the first element of the iterable becomes the seed. Assume the iterable is not indexable. It must use to detect the optionality of the seed. It must make only one pass through the iterable. It must not use reduce. No need to check the preconditions. Your function must pass the below test case, test_reduce0. Hint: exploit the fact that you can use a for-loop with an iterator. Note: sub(xy) will return x-y.
def test_reduce():
try:
my_reduce(sub, [1])
assert False
except TypeError:
assert True
assert my_reduce(None, [1]) == 1
assert my_reduce(sub, []) == None
assert my_reduce(sub, [2, 2]) == 2.