I just wanted to share, you can swap variables without using a temporary variable, just by using a, b = b, a.
Refer to this Screen Link: Learn data science with Python and R projects
a = 6
b = 4
a = b
b = a
print('a: {0}, b: {1}, {2}'.format(a, b, 'Incorrect!'))
Output: a: 4, b: 4, Incorrect!
a = 6
b = 4
temp = a
a = b
b = temp
print('a: {0}, b: {1}, {2}'.format(a, b, 'Correct!'))
Output: a: 4, b: 6, Correct!
a = 6
b = 4
a, b = b, a
print('a: {0}, b: {1}, {2}'.format(a, b, 'Correct!'))
Output: a: 4, b: 6, Correct!
So in the context of the exercise, this function does not swap as intended:
def bad_swap(values, i, j):
values[i] = values[j]
values[j] = values[i]
But this one does:
def swap(values, i, j):
values[i], values[j] = values[j], values[i]