Feb-18-2019, 03:51 AM
(This post was last modified: Feb-18-2019, 04:11 PM by ichabod801.)
Hey I have this homework problem set that I feel like should be very simple but I am stuck:
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])
[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).
This is what I have:
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])
[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).
This is what I have:
def add(vals1, vals2): return (vals1 + vals2)