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 member function in C++ , that takes two lists and return list that contain...
write a member function in C++ , that takes two lists and return list that contain the merge of the two lists in the returned list: first insert the first list and then the second list  
Write a function sublist that takes two lists as arguments, and returns true if the first...
Write a function sublist that takes two lists as arguments, and returns true if the first list appears as a contiguous sublist somewhere within the second list, and false otherwise. > (sublist ’(c d e) ’(a b c d e f g)) #t > (sublist ’(a c e) ’(a b c d e f g)) #f Write a function lgrep that returns the “lines” within a list that contain a given sublist. Use the sublist function implemented in previous exercise...
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)
Write a Scheme function that takes two integers and returns the list of all integer numbers...
Write a Scheme function that takes two integers and returns the list of all integer numbers between these two integers (inclusively) in increasing order. (numbers 10 20) (10 11 12 13 14 15 16 17 18 19 20) Please explain every step.
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 ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT