Question

In: Computer Science

Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers,...

Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers, vals1 and vals2, and that uses recursion to construct and return a new list in which each element is the sum of the corresponding elements of vals1 and vals2. You may assume that the two lists have the same length. For example: >>> add([1, 2, 3], [3, 5, 8]) result: [4, 7, 11] Note that: The first element of the result is the sum of the first elements of the original lists (1 + 3 –> 4). The second element of the result is the sum of the second elements of the original lists (2 + 5 –> 7). The third element of the result is the sum of the third elements of the original lists (3 + 8 –> 11).

Use recursion please / also please explain your codes for better understanding.

Solutions

Expert Solution

Python recursive function code for the problem is provided below, please comment if any doubts:

Note: The code indentation may lose on copying the code, please refer the code screenshot provided at the end if any such problems occurs.

Python code:

#the function definition
def add(vals1, vals2):
#if the lists are empty, return null
if(len(vals1)==0):
return []

#if list has some elements
else:
#add the first elements of the list
x = vals1[0]+ vals2[0];

#delete the both lists first entries
del vals1[0]
del vals2[0]

#call the function recursively
return [x] + add(vals1, vals2)


###function tests
print(add([1, 2, 3], [3, 5, 8]))
print(add([3, 2, 5, 6], [1, 5, 6, 8]))

Output Screenshot:

Code Screenshot:


Related Solutions

This is the question Write a function add(vals1, vals2) that takes as inputs two lists of...
This is the question Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers, vals1 and vals2, and that uses recursion to construct and return a new list in which each element is the sum of the corresponding elements of vals1 and vals2. You may assume that the two lists have the same length. For example: >>> add([1, 2, 3], [3, 5, 8]) result: [4, 7, 11] Note that: The first element of the...
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
Write a function that inputs a list of numbers and a percentile value from 0 to...
Write a function that inputs a list of numbers and a percentile value from 0 to 1 and have the function return the element that is at that percentile (or greater than). You can use this website for the steps https://www.indeed.com/career-advice/career-development/how-to-calculate-percentile check out the step for “Follow these steps to calculate the kth percentile” def percentileReturn(list1,percentile):#function which returns the list of elements which are greater than or equal to the percentile list2=[i for i in list1 if i>=percentile] #creating list2...
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
Write a C++ function template to add two inputs and return their result. Make exceptions for...
Write a C++ function template to add two inputs and return their result. Make exceptions for Characters (such that sum of two characters is a character associated with sum of their ASCII) and String (such that sum of two strings is their concatenation)
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a...
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a list containing only the items in list-a that are not also in list-b. E.g., (remove-all '(a b b c c d) '(a c a)) -> '(b b d)
Create a function that takes two numbers and a mathematical operator +, –, / , *...
Create a function that takes two numbers and a mathematical operator +, –, / , * and will perform a calculation with the given numbers. Examples: calculator(2, "+", 2) ➞ 4 calculator(2, "*", 2) ➞ 4 calculator(4, "/", 2) ➞ 2 Notes If the input tries to divide by 0, return: "Can't divide by 0!" Code in C++ language ...
Write a function myfn6 which takes as inputs vector u and value a, and output as...
Write a function myfn6 which takes as inputs vector u and value a, and output as vector w with its elements being “True, ” or “False, ”(w = [True, False, False, …, True]). Such that “True, ” means a is in u and “False, ” means a is not in u. Test your code for u = [0, -3, 1, 1, 2, 2, 6, 2] and a = 9, a = 1 and a = 2. Copy your code together...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or list) s and an element e, and that calls itself recursively to find and return the index of the last occurrence of e in s. If s is a string, e will be a single-character string; if s is a list, e can be any value. Don’t forget that the index of the first element in a sequence is 0. Important notes: If e...
1.write a small program using a loop to add a series of numbers 2.write a function...
1.write a small program using a loop to add a series of numbers 2.write a function called "main" that performs several given steps. Be sure to call the main() function so that its code executes In python and doesn't have to be long. just long enough to do what it says. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT