Write a function named `vectorSum(l1, l2)` that takes two lists of numbers and adds the numbers in l2 on l1, one by one.

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Write a function named `vectorSum(l1, l2)` that takes two lists of numbers and adds the numbers in l2 on l1, one by one.

Post by answerhappygod »

Write a function named `vectorSum(l1, l2)` that takes two
lists of numbers and adds the numbers in l2 on l1, one by one.
If l2 is shorter than l1, then l2 recycles from the beginning
once it is over. If l2 is longer than l1, then only the first part
of l2 that matches the length of l1 is considered. Your function
must return the summed up resulting list. For example,
vectorSum([1,2,3,4,5], [1,2,3]) returns [2,4,6,5,7] because:
1,2,3,4,5
+ + + + +
1,2,3,1,2 -> Here, l2 recycles from the beginning
= = = = =
2,4,6,5,7
vectorSum([1,2,3], [1,2,3,4,5]) returns [2,4,6] because:
1,2,3
+ + +
1,2,3,4,5 -> Here, only the first 3 items of l2 are used
= = =
2,4,6
vectorSum([1,2,3,4,5], [1,2,1,2,1]) returns [2,4,4,6,6] because:
1,2,3,4,5
+ + + + +
1,2,1,2,1 -> Here, length of l2 matches the length of l1
= = = = =
2,4,4,6,6
You can assume that l1 and l2 are not empty.
"""
def vectorSum(l1, l2):
# remove the following line to solve this question
return
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply