0

Example

num = [0,3,5,3,0,0,9,8] 

Output should be

[3,5,3,9,8,0,0,0]

The solution to this in Python is

def moveZeroes(self, nums):
    zero, cur = -1, 0
    while cur < len(nums):
        if  nums[cur] != 0:
            # update zero index when current is not zero
            zero += 1
            nums[cur], nums[zero] = nums[zero], nums[cur]
        cur += 1

My question is I have seen similar statement like this a lot

nums[cur], nums[zero] = nums[zero], nums[cur]

What is this statement in particular doing?

6 Answers6

1

The idiom

>>> a, b = b, a

swaps the values hold in a and b in one step so that a now points to the value that b used to point to, et vice versa:

>>> a, b = 1, 2
>>> a
1
>>> b
2
>>> a, b = b, a
>>> a
2
>>> b
1

Technically, the right-hand side creates a throwaway tuple, and the left-hand side is a tuple-unpacking assignment.

nums[cur], nums[zero] = nums[zero], nums[cur]

then means "swap the elements at index zero and at index cur.

0

This code nums[cur], nums[zero] = nums[zero], nums[cur] is swapping two values in the nums array. It is the same as if you had written:

temp = nums[cur]
nums[cur] = nums[zero]
nums[zero] = temp

When you use commas on both sides of the = assignment operator, Python essentially makes all of the assignments at once, so you don't need a temp variable.

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • Thank you very much. So is initial value of zero = -1 and cur = 0 in the statement zero, cur = -1, 0 – Rochelle Aug 23 '16 at 05:50
  • Since `zero` and `cur` are array indexes, the swap would fail if either of them had the value `-1`, since that isn't a valid index. But this never happens, because the swap statement in the loop is preceded by `zero += 1`. Also note that the code doesn't swap the *values* of the `zero` and `cur` variables, it swaps the two array elements corresponding to each value. For example if `zero` is `3` and `cur` is `7`, it will swap the fourth and eighth array elements (since arrays indexes start at `0`). – Michael Geary Aug 23 '16 at 05:55
0

It is swapping the values.

a, b = b, a

Now a has the value of b, and b has the value of a

Jahongir Rahmonov
  • 13,083
  • 10
  • 47
  • 91
0

It's a variable swap, Python-style. It constructs a tuple of two values, the values at zero and cur, then unpacks that tuple and assigns the values to the indices at cur and zero; by reversing the indices assigned to, it swaps the values.

Note that the code could be simplified dramatically (though possibly at the expense of running slightly slower) by repurposing the key argument of the sort method to let Python do the work at the C layer:

def moveZeroes(self, nums):
    nums.sort(key=lambda x: x == 0)
    # Or more efficiently but obscurely:
    # nums.sort(key=(0).__eq__)
    # Or with an operator import to get speed with less obscurity:
    # nums.sort(key=operator.not_)

Since Python's TimSort algorithm is stable, this preserves the order for all the non-zero values while moving the zeroes to the end.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
0

This is Python's tuple packing and unpacking feature.

It's functionally identical to

temp = nums[cur]
nums[cur] = nums[zero]
nums[zero] = temp

but more efficient to the computer and more readable to programmers.

Further reading: https://stackoverflow.com/a/14836456/5399734

Community
  • 1
  • 1
nalzok
  • 14,965
  • 21
  • 72
  • 139
0

It is first evaluating both of the values on the right, then it is substituting them to the matching slots on the left. So basically:

a, b = b, a

is like:

c = b
d = a
a = c
b = d

It is particularly useful to swap values or perform some operations on them without creating additional variables.

Adrian Jałoszewski
  • 1,695
  • 3
  • 17
  • 33