0

Happy New Year. I have a question about swapping elements in an array under Python environment. The code w/o issues are:

def findMissingNumber(nums):
    length = len(nums)
    result = []
    i = 0
    while i < length:
        # num = nums[i]
        target = nums[i] - 1
        if nums[i] != i+1 and nums[i] != nums[target]:
            # swap with the nums[i]-1
            nums[target], nums[i]= nums[i], nums[target]
        else:
            i += 1
    for j in range(length):
        if nums[j] != j+1:
            result.append(j+1)
    return result

print(findMissingNumber([2, 3, 1, 8, 2, 3, 5, 1]))
print(findMissingNumber([2, 4, 1, 2]))
print(findMissingNumber([2, 3, 2, 1]))

with answer:

[4, 6, 7]
[3]
[4]

The wrong one is:

def findMissingNumber(nums):
    length = len(nums)
    result = []
    i = 0
    while i < length:
        num = nums[i]
        target = num - 1
        if num != i+1 and num != nums[target]:
            # swap with the num-1
            nums[target], num = num, nums[target]
        else:
            i += 1
    for j in range(length):
        if nums[j] != j+1:
            result.append(j+1)
    return result

print(findMissingNumber([2, 3, 1, 8, 2, 3, 5, 1]))
print(findMissingNumber([2, 4, 1, 2]))
print(findMissingNumber([2, 3, 2, 1]))

with answer:

[4, 6, 7]
[3, 4]
[3, 4]

I am not sure why the reference (should I call that) causing trouble. Would you mind explaining it in detail.
Thanks.

Qiang Super
  • 323
  • 1
  • 11

1 Answers1

1

Fixed code with comments.

def findMissingNumber(nums):
    length = len(nums)
    result = []
    i = 0
    while i < length:
        # num and nums[i] have the same value but doesnt refer to same memory location
        num = nums[i]
        target = num - 1
        if num != i+1 and num != nums[target]:
            # swap with the num-1
            
            # incorrect swap, num value is not referencing the list index, but has a copy of its value,
            # so instead of swapping value with num, do it from the list like below
            # nums[target], num = num, nums[target]
            
            
            # correct swap
            nums[target], nums[i] = nums[i], nums[target]
        else:
            i += 1
    for j in range(length):
        if nums[j] != j+1:
            result.append(j+1)
    return result

print(findMissingNumber([2, 3, 1, 8, 2, 3, 5, 1]))
print(findMissingNumber([2, 4, 1, 2]))
print(findMissingNumber([2, 3, 2, 1]))
gsb22
  • 2,112
  • 2
  • 10
  • 25
  • Thanks. Would you mind talking more about the difference between num and nums[i]. I still dont quite understand about it. – Qiang Super Jan 05 '21 at 19:30
  • @QiangSuper When you `num = nums[i]` , what you are doing is taking `nums[i]` value and then creating a new variable and assigning it the value. Now num and nums[i] are separate reference. So, whatever you do with num wont reflect in `nums[i]` and that's why the swap wasn't reflecting in `nums` list. – gsb22 Jan 06 '21 at 10:23
  • Thanks for your explanation. Does it mean that a = b in Python is to copy the value from b to a, but it won't create a link relationship between them? – Qiang Super Jan 07 '21 at 02:51
  • @QiangSuper, no, actually a = b, both will point to same location because you are referencing the whole variable. So, in above case, if you had created `numbers = nums`, then any changes made on `numbers` would reflect on `nums` as well – gsb22 Jan 07 '21 at 05:29
  • Thanks for your help. I try to summarize the results you shared. If you assign a value to a new variable, like a = b[0]. a is a new location in memory, so that any change to a won't impact b[0]. However, if you assign a new reference to the whole variable, say a = b, a still point to the location of b in memory and any change in b will impact on b. Am I correct? Might I ask why? Appreciate. – Qiang Super Jan 07 '21 at 19:20
  • what time zone you live in? let's have a google meet call during, I can explain you better over call. Because, what you wrote is true but there are some more points to that. How python handles mutable data types. – gsb22 Jan 07 '21 at 20:36
  • Thanks. I am 3pm now. Feel free to let me know when will it work for you. – Qiang Super Jan 07 '21 at 20:39
  • lol, we have like 12 hours time difference. let's meet 5:30 pm GMT? – gsb22 Jan 08 '21 at 12:32