* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2.9 KiB
2.9 KiB
id, title, challengeType, videoUrl, dashedName
id | title | challengeType | videoUrl | dashedName |
---|---|---|---|---|
599c333915e0ea32d04d4bec | 元素操作 | 5 | element-wise-operations |
--description--
实现基本的元素矩阵 - 矩阵和标量矩阵运算。
实行:
:: *另外
:: *减法
:: *乘法
:: *分裂
:: *取幂
第一个参数是要执行的操作,例如:用于矩阵加法的“m_add”和用于标量加法的“s_add”。第二和第三参数将是要在其上执行操作的矩阵。
--hints--
operation
是一种功能。
assert(typeof operation === 'function');
operation("m_add",[[1,2],[3,4]],[[1,2],[3,4]])
应返回[[2,4],[6,8]]
。
assert.deepEqual(
operation(
'm_add',
[
[1, 2],
[3, 4]
],
[
[1, 2],
[3, 4]
]
),
[
[2, 4],
[6, 8]
]
);
operation("s_add",[[1,2],[3,4]],[[1,2],[3,4]])
应返回[[3,4],[5,6]]
。
assert.deepEqual(
operation(
's_add',
[
[1, 2],
[3, 4]
],
2
),
[
[3, 4],
[5, 6]
]
);
operation("m_sub",[[1,2],[3,4]],[[1,2],[3,4]])
应返回[[0,0],[0,0]]
。
assert.deepEqual(
operation(
'm_sub',
[
[1, 2],
[3, 4]
],
[
[1, 2],
[3, 4]
]
),
[
[0, 0],
[0, 0]
]
);
operation("m_mult",[[1,2],[3,4]],[[1,2],[3,4]])
应该返回[[1,4],[9,16]]
。
assert.deepEqual(
operation(
'm_mult',
[
[1, 2],
[3, 4]
],
[
[1, 2],
[3, 4]
]
),
[
[1, 4],
[9, 16]
]
);
operation("m_div",[[1,2],[3,4]],[[1,2],[3,4]])
应返回[[1,1],[1,1]]
。
assert.deepEqual(
operation(
'm_div',
[
[1, 2],
[3, 4]
],
[
[1, 2],
[3, 4]
]
),
[
[1, 1],
[1, 1]
]
);
operation("m_exp",[[1,2],[3,4]],[[1,2],[3,4]])
应返回[[1,4],[27,256]]
。
assert.deepEqual(
operation(
'm_exp',
[
[1, 2],
[3, 4]
],
[
[1, 2],
[3, 4]
]
),
[
[1, 4],
[27, 256]
]
);
operation("m_add",[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16]])
应该返回[[10,12,14,16],[18,20,22,24]]
。
assert.deepEqual(
operation(
'm_add',
[
[1, 2, 3, 4],
[5, 6, 7, 8]
],
[
[9, 10, 11, 12],
[13, 14, 15, 16]
]
),
[
[10, 12, 14, 16],
[18, 20, 22, 24]
]
);
--seed--
--seed-contents--
function operation(op, arr1, arr2) {
}
--solutions--
function operation(op, arr1, arr2) {
const ops = {
add: ((a, b) => a + b),
sub: ((a, b) => a - b),
mult: ((a, b) => a * b),
div: ((a, b) => a / b),
exp: ((a, b) => Math.pow(a, b))
};
const ifm = op.startsWith('m');
const doOp = ops[op.substring(2)];
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr1[0].length; j++) {
arr1[i][j] = doOp(arr1[i][j], (ifm) ? (arr2[i][j]) : (arr2));
}
}
return arr1;
}