+ 4

Can you please explain the output of this code?

arr = [1, 2, 3, 4, 3, 2, 1] a = list(map(lambda x:x % 2 == 0,arr)) if a [3] == True: print("c") else print("d") Answer is c

8th Mar 2020, 2:22 PM
APC (Inactive for a while)
APC (Inactive for a while) - avatar
4 Answers
+ 3
What about a[3] and a[0] ?
8th Mar 2020, 2:34 PM
APC (Inactive for a while)
APC (Inactive for a while) - avatar
+ 2
a[0] = 1 = odd = "d" a[1] = 2 = even = "c" a[2] = 3 = odd = "d" a[3] = 4 = even = "c" a[4] = 3 = odd = "d" ... etc.
8th Mar 2020, 2:37 PM
Brylle Olaivar
Brylle Olaivar - avatar
+ 2
the 'map' maps each element in the list 'arr' to a co-domain defined on the function 'lambda x:x%2==2' that tests for even number. Since the index 3 on the list 'arr' is even, Where a[0] is 1, odd a[1] is 2, even a[2] is 3, odd a[3] is 4, even the boolean tested true. Therefore, the string 'c' is printed
9th Mar 2020, 9:47 PM
Uthman Abdulrahman
+ 1
The list a contains True and False values. Since you mapped arr with lambda x: x%2 == 0, it returns True when a value of arr is an even number and False if not arr[3] is an even number, so it prints "c" arr[0] however is an odd number, so it prints "d"
8th Mar 2020, 2:31 PM
Brylle Olaivar
Brylle Olaivar - avatar