Files

2.0 KiB
Raw Permalink Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5a23c84252665b21eecc7eb1 Одинична матриця 5 302290 identity-matrix

--description--

Одинична матриця це квадратна матриця розміру \( n \times n \), де всі діагональні елементи 1і (одиниці), а всі інші елементи 0і (нулі).

  • \(\displaystyle I_{n}=\begin{bmatrix} 1 & 0 & 0 \cr 0 & 1 & 0 \cr 0 & 0 & 1 \cr \end{bmatrix}\)

--instructions--

Напишіть функцію, яка приймає число n як параметр і повертає одиничну матрицю порядку \( n \times n \).

--hints--

idMatrix має бути функцією.

assert(typeof idMatrix == 'function');

idMatrix(1) має повернути масив.

assert(Array.isArray(idMatrix(1)));

idMatrix(1) має повернути [ [ 1 ] ].

assert.deepEqual(idMatrix(1), results[0]);

idMatrix(2) має повернути [ [ 1, 0 ], [ 0, 1 ] ].

assert.deepEqual(idMatrix(2), results[1]);

idMatrix(3) має повернути [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ].

assert.deepEqual(idMatrix(3), results[2]);

idMatrix(4) має повернути [ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ].

assert.deepEqual(idMatrix(4), results[3]);

--seed--

--after-user-code--

let results=[[ [ 1 ] ],
[ [ 1, 0 ], [ 0, 1 ] ],
[ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ],
[ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ]]

--seed-contents--

function idMatrix(n) {

}

--solutions--

function idMatrix(n) {
    return Array.apply(null, new Array(n)).map(function (x, i, xs) {
        return xs.map(function (_, k) {
            return i === k ? 1 : 0;
        })
    });
}