-2

I'm current working on missing number, and I ran into a strange error.

def find_missing_number(nums):

  i = 0

  while i < len(nums):

    #print(nums)


    if nums[i] < len(nums) and nums[i] != nums[nums[i]]:
      nums[nums[i]], nums[i] = nums[i], nums[nums[i]]
      #nums[i], nums[nums[i]] = nums[nums[i]], nums[i]  # THIS ONE GIVES AN ERROR?????




      # temp = nums[nums[i]]
      # nums[nums[i]] = nums[i]
      # nums[i] = temp
    else:
      i+=1



I keep getting a list assignment index out of range for the line that I indicated gives an error.

Does anyone know why? I have an if statement making sure it won't access any values out of range.

Edit: I'm trying to the the Missing Number problem on Leetcode and use cyclic sort to sort nums. These are the test cases:

Input: [4, 0, 3, 1] Output: 2

Input: [8, 3, 5, 2, 4, 6, 0, 1] Output: 7

Thanks!

cam
  • 11
  • 1
  • Testcase 1: Input: [4, 0, 3, 1] - Output: 2 Testcase 2: Input: [8, 3, 5, 2, 4, 6, 0, 1] - Output: 7 – cam Sep 03 '22 at 01:34
  • 1
    Can you explain the *Problem* clearer - so we know what's your approach? It's not clear to us what is the *find missing number*... – Daniel Hao Sep 03 '22 at 01:36
  • Agreed, this question is extremely vague. I have no clue what is meant by a "missing number", but an explanation would be great. – rv.kvetch Sep 03 '22 at 01:41
  • @JeffreyRam I see it now, but others may not see it. I actually *didn't* until you pointed it out just now, a bit forcefully of course. I still think others might be a bit confused is all. – rv.kvetch Sep 03 '22 at 01:43
  • Yeah sorry about that y'all, it's the Missing Number problem on leetcode and I'm trying to do cyclic sort to sort nums. Just added it to the question. – cam Sep 03 '22 at 01:44
  • 1
    @rv.kvetch Yeah I agree that his question is confusing because it seems to be more like *"Why does this give me error"* instead of *"How to find missing number"*. I think he should simplify it without unnecessary background info – Jeffrey Ram Sep 03 '22 at 01:51
  • I don't need help with the problem though, I want to know why that line that I commented out gives an error. – cam Sep 03 '22 at 01:55

1 Answers1

1

You are assigning a variable used for the index value for an array at the same time you are using that index to select a value from the array. Even if it works one way I wouldn't rely on it. An optimization in the runtime could render the assignment different. Just use some temporary variables and two statements.

numsA, numsB = nums[i], nums[nums[i]]
nums[numsA], nums[i] = numsA, numsB
kd4ttc
  • 1,075
  • 1
  • 10
  • 28
  • I'd use `nums[numsA]` on the second line as well... – Jiří Baum Sep 03 '22 at 02:15
  • I think technically Python does define the order of the operations, so a new version shouldn't change it, but if you have to look it up, it's in any case a code clarity issue... – Jiří Baum Sep 03 '22 at 02:17
  • I grew up in the old days (1970s) when languages didn't morph. Python has been rather loosely curated, you might say. As to nums[numsA], yes. My bad. Good catch. – kd4ttc Sep 03 '22 at 02:29
  • At a minimum, the order is documented: https://docs.python.org/3/reference/expressions.html#evaluation-order – Jiří Baum Sep 03 '22 at 02:32
  • But if the values are really evaluated first on the Right to make a tuple, then why did the user find that assignment order mattered? – kd4ttc Sep 03 '22 at 02:38
  • in `expr3, expr4 = expr1, expr2` the value `expr3` is assigned before `expr4` is evaluated? – Jiří Baum Sep 03 '22 at 02:43
  • 1
    NatRiddle pointed out a nice example that deal with this case exactly. Yes, expr3 does get assigned before expr4. The problem line in the original question first mutates nums[i] which then is changed when nums[nums[i]] is evaluated. Like I had explained, he was assigning the index variable in the same statement where it was later used. The reference NatRiddle was a good read. – kd4ttc Sep 03 '22 at 02:50