I was trying to understand how python reduce built-in method works, so was trying some easy samples I can think of. But here is an example I don't quite understand, I'll be really appreciate if someone can give me some hints here.
So I have an array which has several prime numbers like this
>>> arr
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
And I try to use reduce function like this
>>> reduce(lambda x, y: is_prime(x) and is_prime(y), arr)
False
I don't quite understand why the answer here is False instead of True as I am just anding all the answer of is_prime and all the numbers in the array is prime which would return True for every is_prime.
I also checked that every number in the array actually returns True for my is_prime()
>>> for n in arr:
... if not is_prime(n):
... print n
...
>>>
I must have missing something here, could anyone help? Thanks.