samedi 25 mai 2019

If condition is not checked multiple times inside a while loop

I am trying to find the number of times a number is shifted from its original sequence. I swap the number in ascending order and I count the number of swaps; if the count is >3 I break from the loop.

But so far when I check the numbers the first iteration only the if condition executes and after that it doesn't check for the condition. I am trying to understand why the if condition isn't executing.

 def minimumBribes(q):
       x=q.copy()
       lis=[]
       i=0
       j=0
       count=0
       flag=0
       while i< len(q):
           k=j+1
           a=q[j]
           b=q[k]
           print(a,b)
           if a>b:
           print(a>b)
           x.pop(x.index(a))
           x.insert(x.index(b),a)

           print(x)
           flag+=1
           if flag>3:
              print('Too chaotic')
              break
           else:
              j+=1
           i+=1
       print(count)


if __name__ == '__main__':
    t = int(input())

    for t_itr in range(t):
        n = int(input())

        q = list(map(int, input().rstrip().split()))

        minimumBribes(q)

Aucun commentaire:

Enregistrer un commentaire