Computer Science, asked by junaidanoufia, 5 hours ago

write a functiom name add list () that takes two list L and M of the same size as arguments and adds their elements together to form a new list N wjose sums are the corresponding to the list L and M​

Answers

Answered by Equestriadash
1

The following co‎des have been written using Python.

\tt import\ numpy\ as\ np\\de f\ add\_list(l,\ m):\\{\ \ \ \ \ }n\ =\ list()\\{\ \ \ \ \ }if\ len(l)\ ==\ len(m):\\{\ \ \ \ \ }{\ \ \ \ \ }a\ =\ np.array(l)\\{\ \ \ \ \ }{\ \ \ \ \ }b\ =\ np.array(m)\\{\ \ \ \ \ }{\ \ \ \ \ }n\ =\ list(a\ +\ b)\\{\ \ \ \ \ }{\ \ \ \ \ }print(n)

We import a module, \tt numpy [NumPy - Numerical Python], so as to convert the upcoming lists into arrays, and then add them. Addition in lists isn't the same as addition in arrays. In lists, when two lists are joined by the '+' operator, both the lists become one single list. Whereas in arrays, when two arrays [converted from lists] are joined by the '+' operator, the elements in them are added up. Once we import the module to perform this task, we define a function using \tt d ef which is used to create user-defined functions. We assign 'n' to an empty list, to represent the final list with the sum. We check for the equality in number of elements for both the lists using a conditional statement and then perform the conversion into arrays, to add the elements. We assign the final list back into 'n' and print it in list form.

Similar questions