Q#9: Identity Matrix

Can you build a function that prints an identity matrix, given the identity matrix size?

Examples:

Input: 2
Output:
1 0
0 1
Input: 4
Output:
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

TRY IT YOURSELF

Answer:

Using numpy the answer is simple and straightforward:

import numpy as np
input = int(input())
np.eye(input)

--

--