Question: in python 4. Write a function diff(valsi, vals2) that takes as inputs two arbitrary lists of non-negative integers vals1 and vals2 and uses recursion to

 in python 4. Write a function diff(valsi, vals2) that takes asinputs two arbitrary lists of non-negative integers vals1 and vals2 and uses

in python

4. Write a function diff(valsi, vals2) that takes as inputs two arbitrary lists of non-negative integers vals1 and vals2 and uses recursion to construct and return a new list in which each element is the the absolute value of the difference of the corresponding elements from the original lists. For example: >>> diff([3, 4, 2, 5], [7, 2, 9, 5]) result: [4, 2, 7, 0] In the return value for this case: The first element is 4, because the difference of the first elements in the original lists is 3 - 7 = -4, and we multiply by -1 to get the absolute value. The second element is 2, because the difference of the second elements in the original lists is 4 - 2 = 2, and we don't need to adjust it because 2 is already non-negative. The third element is 7, because 2 - 9 = -7, and we multiply by -1 to get the absolute value. The fourth element is 0, because 5 - 5 = 0. Important You may not use Python's built-in function for computing the absolute value of a number. Rather, you will need to use conditional execution to determine whether you need to adjust the result of subtracting a given pair of elements. Here are two other examples: >>> diff([5, 6, 2], [1, 9, 15]) result: [4, 3, 13] >>> diffC[], []) result: [] If one of the lists is longer than the other, its "extra" elements - the ones with no counterparts in the shorter list - should appear immediately after the differences (if any) in the returned list. For example: list - should appear immediately after the differences (if any) in the returned list. For example: >>> diff([0, 3, 6, 9], [1, 1]) result: [1, 2, 6, 9] >>> diff([3, 1, 9], [2, 4, 5, 7]) result: [1, 3, 4, 7]) >>> diff([1, 2], []) result: [1, 2] >>> diff([], [3, 4, 5]) result: [3, 4, 5] Important: When constructing a list using the + operator, make sure that both of the operands are lists. For example, if x is an integer and vals is a list, the following will produce an error: X + vals # error: cannot add an integer to a list Instead, you would need to turn x into a list before adding it: [x] + vals # correct! Don't forget to test your functions using the approaches mentioned at the start of Problem 3

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!