Weird python behavior in today div3E

Правка en1, от misaki646, 2024-10-25 08:09:47

Today I find that python swap value hack doesn't work as intended all the time. Maybe the best practice is just write code line by line instead of this? If can someone please explain to me is there anything wrong?

Intention: Swap a and b

Python: a, b = b, a (usually works all the time and I actually use this in today div3C)

But this time it doesn't work as intended

    for _ in range(int(input())):
        n = int(input())
        a = list(map(int, input().split()))
        a = [x-1 for x in a]
        res = 0
        for i in range(n):
            if a[i]>=0:
                mlen = 0
                ix = i
                while a[ix]>=0:
                    a[ix], ix = -1, a[ix]
                    #ix, a[ix] = a[ix], -1 # doesn't work the same as above
                    mlen+=1
                res += (mlen-1)//2
        print(res)

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский misaki646 2024-10-25 08:20:21 49
en1 Английский misaki646 2024-10-25 08:09:47 928 Initial revision (published)