mardi 26 janvier 2021

Two if statements in a for loop?

class Solution:
    def transformArray(self, arr: List[int]) -> List[int]:
        x=arr
        while True:
            f=True
            for i in range(1,len(arr)-1):
                if arr[i-1]<arr[i] and arr[i]>arr[i+1]:
                    f=False
                    x[i]=x[i]-1
                    print(x[i])
                if arr[i-1]>arr[i] and arr[i]<arr[i+1]:
                    f=False
                    x[i]=x[i]+1
                    print(x[i])
                
            #print(x)
            x=arr
            if f==True:
                break
        return x

In the above code both the if statements don't execute , only the second one does. I have tried using elif but it still doesn't work. What am i missing here?

Aucun commentaire:

Enregistrer un commentaire