M3) Replacing negative tuple values
A measurement tuple contains integers. zero_negatives(values) must return a tuple with the same length and order, replacing each negative value with 0 and retaining every other value. The input tuple must remain unchanged.
For values = (1, -2, 3, -4, 5), the required result is (1, 0, 3, 0, 5). Empty input must return ().
Compare the four implementations in Listing 1.
Which implementation satisfies the stated requirements?
TipHint
len(values)returns the number of items.range(stop)visits integers from0up to, but excluding,stop:list(range(2))is[0, 1].listcollects values into a new list, andrange(0)is empty.result.append(value)adds one value to the end of the list and returnsNone; appending6to[4]changes it to[4, 6].tuple(result)creates a tuple containing the list’s items in order:tuple([4, 6])is(4, 6).