Home python Explain the SWAP function

Explain the SWAP function

Author

Date

Category

Explain how the SWAP function works, which makes each line of this function.
Thanks in advance.

def sorte (num):
  For i in Range (Len (NUM)):
    L = I.
    For k in Range (I + 1, Len (NUM)):
      IF NUM [K] & LT; NUM [L]:
        L = K.
    SWAP (Num, L, I)
DEF swap (V, X, Y):
  T = V [x]
  V [x] = V [y]
  V [Y] = T
LST = [2,235,654,0,434687,754,32,1]
K = max_num (LST)
Sorte (LST)
Print (K)
Print (LST)

Answer 1, Authority 100%

The function takes a list V and changes the elements with indices x and y in it with places.
Right:

t = v [x] # save the Xth element in a temporary variable
V [x] = v [y] # write in the X-si-sip position of an array value from y-th element
v [y] = t # write to the Y-th position
       # which was saved in a temporary variable

The most ridiculous thing is that for this it was not necessary to make a whole function, everything could be written in one line using the assignment with unpacking:

v [x], v [y] = v [y], v [x]

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions