I have the following code for a small program:
def get_different_number(arr):
if len(arr) == 0 or arr is None:
return 0
n = len(arr)
for i in range(n):
print (str(i))
temp = arr[i]
while (temp < n and temp != arr[temp]):
temp, arr[temp] = arr[temp],temp
for i in range(n):
if arr[i] != i:
return i
return n
test = [0,1,2,4,5]
get_different_number(test)
however, when it executes, it tells me there is a problem in the line where I swap temp and arr[temp], it gives me a list index out of range error at i is 3 (temp is 4).
When I change that line to arr[temp], temp = arr[temp], temp (reversed assign order), it worked fine. Why is this happening? I thought the a,b = b,a assignment in python assign both elements at the same time?