I recently received the following question in a Hackerrank that I wasn't able to answer and I haven't found the correct answer anywhere else.
Complete the lambda map function: given a array such as [[1,2,3,-1,2],[2,4,-3]] take the square of all the non-negative numbers in the two arrays. The output is suppose to be:
[1,4,9,4]
[4,16]
Additionally, the array will also contain an additional number at the beginning of the array which is the length of the array. So in the above example, the length is 2 so the final array that will be passed through the function will be [2, [1,2,3,-1,2],[2,4,-3]]. (We don't want to take the square of the initial number, in this case 2)
def lambdamap(arr):
final = map(
#write your lambda function here
, arr)
return final
create a lambda function within the map that creates the desired output.
I have tried a number of different ways such as
map(lambda x: x**2, filter(lambda x: x>0, arr))
But this does not work on nested lists like the array that is passed through the function. Please let me know if there is any information I can provide to help clarify the question.