Skip to content
DeepForgeby svx
All problems
EasyLinear Algebrala-154

Permutation Matrix

Problem

Build the permutation matrix P for the permutation p, where p[i] is the destination column of row i:

P[i][j] = 1 if j == p[i] else 0.

Multiplying P by a vector permutes its entries according to p.

Hint

Each row has exactly one 1, in column p[i].

Test cases

case 1
in: [[0,1]]
expected: [[1,0],[0,1]]
case 2
in: [[1,0]]
expected: [[0,1],[1,0]]
case 3
in: [[1,2,0]]
expected: [[0,1,0],[0,0,1],[1,0,0]]
case 4
in: [[2,1,0]]
expected: [[0,0,1],[0,1,0],[1,0,0]]
Starter codePython
def permutation_matrix(p):
    # Your code here
    pass

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