Problem

8 /12


Diagonal Array

Theory Click to read/hide

Diagonal Arrays

The  diag(V, k=0) function allows you to extract a diagonal from an array, as well as build diagonal arrays from one-dimensional arrays.
V - An array-like object, two-dimensional or one-dimensional arrays, matrices, lists, or tuples, or any function or object with a method that returns a list or tuple.
k - index of the diagonal (optional).
The default is k = 0 which corresponds to the main diagonal. A positive k value moves the diagonal up, a negative value moves it down.

The function returns array NumPy (ndarray) - the specified array diagonal or a diagonal array from the specified one-dimensional array.

Problem

The input is the number n. Output an array of size nxn, in in the diagonal numbers are 0 to n-1, and the rest of the numbers are 0.
 

 

Examples
# Input Output
1 5 [[0 0 0 0 0]
 [0 1 0 0 0]
 [0 0 2 0 0]
 [0 0 0 3 0]
 [0 0 0 0 4]]