-4

I am trying to solve a leetcode medium (monotonic array):

def isMonotonic(array):
    # Write your code here.
    toggle = (array[0]-array[1])>=0
    for i in range(1, len(array)-1):
        if (toggle!=(array[i]-array[i+1])>0):
            print(toggle, " == ", (array[i]-array[i+1])>0)
            print(toggle == (array[i]-array[i+1])>0) #Gives False when should give True on True == True
            print(True == True)
    return True

For Array = [-1, -5, -10, -1100, -1100, -1101, -1102, -9001]

Gives Output:

True  ==  True
False
True

Link to Python Playground with code: Programiz python env with code to replicate the error

In line:

print(toggle == (array[i]-array[i+1])>0) #Gives False when should give True on True == True

Solution:

Replace:

print(toggle == (array[i]-array[i+1])>0)

With:

curr = (array[i]-array[i+1])>0
print(toggle == curr)
wjandrea
  • 28,235
  • 9
  • 60
  • 81

0 Answers0