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

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
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)
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...
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.
Write a function that takes a numeric or integer vector and adds up only the numbers...
Write a function that takes a numeric or integer vector and adds up only the numbers whose integer parts are even. Modify your answer to question above to include an option that allows you to choose whether to sum numbers whose integer parts are even or are odd. Your function should have as a default that it gives the same output as the function in question 4. In other words, if the user doesn’t specify whether to sum evens or...
Write a Python program to add, multiply and divide any two numbers.
Write a Python program to add, multiply and divide any two numbers.
Write a function in Python that adds two Binary Trees together. The inputs of your function...
Write a function in Python that adds two Binary Trees together. The inputs of your function should be two binary trees (or their roots, depending on how you plan on coding this), and the output will be one Binary Tree that is the sum of the two inputted. To add Binary Trees together: Add the values of nodes with the same position in both trees together, this will be the value assigned to the node of the same position in...
In Python, Q. Write a function max_increase(seq) which takes as argument a sequence of numbers and...
In Python, Q. Write a function max_increase(seq) which takes as argument a sequence of numbers and returns the maximum increase from one element in the sequence to an element at a higher index. Assumptions and restrictions: The function must return a number. If there is no increasing pair in the sequence, the function should return 0. This may happen for example if the sequence is decreasing, or if it contains fewer than 2 elements. You can assume that the argument...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT