Skip to content
DeepForgeby svx
All problems
EasyLinear Algebrala-108

Swap Matrix Rows

Problem

Return a copy of matrix A with rows i and j swapped.

If i == j, return the matrix unchanged.

Hint

Copy first so the input matrix is not mutated.

Test cases

case 1
in: [[[1,2],[3,4]],0,1]
expected: [[3,4],[1,2]]
case 2
in: [[[1,2,3],[4,5,6],[7,8,9]],0,2]
expected: [[7,8,9],[4,5,6],[1,2,3]]
case 3
in: [[[1,2],[3,4]],1,1]
expected: [[1,2],[3,4]]
case 4
in: [[[1,2],[3,4]],1,0]
expected: [[3,4],[1,2]]
Starter codePython
def swap_rows(A, i, j):
    # Your code here
    pass

4 test cases · run your code against them in the editor