Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedimage text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Filing And Computer Database Projects

Authors: Jeffrey Stewart

2nd Edition

007822781X, 9780078227813

More Books

Students also viewed these Databases questions

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago