I was working on this Leetcode problem. I managed to solve it and later looked upon the solution to find this :-
class Solution(object):
## A is the Given Array
def isMonotonic(self, A):
return (all(A[i] <= A[i+1] for i in range(len(A) - 1)) or
all(A[i] >= A[i+1] for i in range(len(A) - 1)))
My doubt is regarding the usage of all() in this manner - all(A[i] <= A[i+1] for i in range(len(A) - 1))
, clearly all() is being called with a boolean parameter and it works. But when I executed all(True)
in python console I got TypeError: 'bool' object is not iterable
. So basically how did the usage of all() in this manner work in the code but not in the console.
My another doubt is in A[i] <= A[i+1] for i in range(len(A) - 1)
. When i
reaches the last index value of the list shouldn't A[i] <= A[i+1]
raise an IndexError: list index out of range
.