feat(curriculum): restore seed + solution to Chinese (#40683)
* 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>
This commit is contained in:
committed by
GitHub
parent
0095583028
commit
ee1e8abd87
@ -3,6 +3,7 @@ id: 59713da0a428c1a62d7db430
|
||||
title: 克莱默的统治
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: cramers-rule
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -29,5 +30,118 @@ assert.deepEqual(cramersRule(matrices[0], freeTerms[0]), answers[0]);
|
||||
assert.deepEqual(cramersRule(matrices[1], freeTerms[1]), answers[1]);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
const matrices = [
|
||||
[
|
||||
[2, -1, 5, 1],
|
||||
[3, 2, 2, -6],
|
||||
[1, 3, 3, -1],
|
||||
[5, -2, -3, 3]
|
||||
],
|
||||
[
|
||||
[3, 1, 1],
|
||||
[2, 2, 5],
|
||||
[1, -3, -4]
|
||||
]
|
||||
];
|
||||
const freeTerms = [[-3, -32, -47, 49], [3, -1, 2]];
|
||||
|
||||
const answers = [[2, -12, -4, 1], [1, 1, -1]];
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function cramersRule(matrix, freeTerms) {
|
||||
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
/**
|
||||
* Compute Cramer's Rule
|
||||
* @param {array} matrix x,y,z, etc. terms
|
||||
* @param {array} freeTerms
|
||||
* @return {array} solution for x,y,z, etc.
|
||||
*/
|
||||
function cramersRule(matrix, freeTerms) {
|
||||
const det = detr(matrix);
|
||||
const returnArray = [];
|
||||
let i;
|
||||
|
||||
for (i = 0; i < matrix[0].length; i++) {
|
||||
const tmpMatrix = insertInTerms(matrix, freeTerms, i);
|
||||
returnArray.push(detr(tmpMatrix) / det);
|
||||
}
|
||||
return returnArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts single dimensional array into
|
||||
* @param {array} matrix multidimensional array to have ins inserted into
|
||||
* @param {array} ins single dimensional array to be inserted vertically into matrix
|
||||
* @param {array} at zero based offset for ins to be inserted into matrix
|
||||
* @return {array} New multidimensional array with ins replacing the at column in matrix
|
||||
*/
|
||||
function insertInTerms(matrix, ins, at) {
|
||||
const tmpMatrix = clone(matrix);
|
||||
let i;
|
||||
for (i = 0; i < matrix.length; i++) {
|
||||
tmpMatrix[i][at] = ins[i];
|
||||
}
|
||||
return tmpMatrix;
|
||||
}
|
||||
/**
|
||||
* Compute the determinate of a matrix. No protection, assumes square matrix
|
||||
* function borrowed, and adapted from MIT Licensed numericjs library (www.numericjs.com)
|
||||
* @param {array} m Input Matrix (multidimensional array)
|
||||
* @return {number} result rounded to 2 decimal
|
||||
*/
|
||||
function detr(m) {
|
||||
let ret = 1;
|
||||
let j;
|
||||
let k;
|
||||
const A = clone(m);
|
||||
const n = m[0].length;
|
||||
let alpha;
|
||||
|
||||
for (j = 0; j < n - 1; j++) {
|
||||
k = j;
|
||||
for (let i = j + 1; i < n; i++) { if (Math.abs(A[i][j]) > Math.abs(A[k][j])) { k = i; } }
|
||||
if (k !== j) {
|
||||
const temp = A[k]; A[k] = A[j]; A[j] = temp;
|
||||
ret *= -1;
|
||||
}
|
||||
const Aj = A[j];
|
||||
for (let i = j + 1; i < n; i++) {
|
||||
const Ai = A[i];
|
||||
alpha = Ai[j] / Aj[j];
|
||||
for (k = j + 1; k < n - 1; k += 2) {
|
||||
const k1 = k + 1;
|
||||
Ai[k] -= Aj[k] * alpha;
|
||||
Ai[k1] -= Aj[k1] * alpha;
|
||||
}
|
||||
if (k !== n) { Ai[k] -= Aj[k] * alpha; }
|
||||
}
|
||||
if (Aj[j] === 0) { return 0; }
|
||||
ret *= Aj[j];
|
||||
}
|
||||
return Math.round(ret * A[j][j] * 100) / 100;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone two dimensional Array using ECMAScript 5 map function and EcmaScript 3 slice
|
||||
* @param {array} m Input matrix (multidimensional array) to clone
|
||||
* @return {array} New matrix copy
|
||||
*/
|
||||
function clone(m) {
|
||||
return m.map(a => a.slice());
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user