fix(curriculum): remove before/after-user-code from rosetta challenges 46-48 (#66431)

This commit is contained in:
Sem Bauke
2026-03-13 19:34:38 +01:00
committed by GitHub
parent 975706e5e3
commit b8c6ce977c
3 changed files with 255 additions and 293 deletions

View File

@@ -10,6 +10,79 @@ dashedName: sudoku
Write a function to solve a partially filled-in normal 9x9 Sudoku grid and return the result. The blank fields are represented by `-1`.
# --before-each--
```js
const puzzle1 = [
[8, 1, 9, -1, -1, 5, -1, -1, -1],
[-1, -1, 2, -1, -1, -1, 7, 5, -1],
[-1, 3, 7, 1, -1, 4, -1, 6, -1],
[4, -1, -1, 5, 9, -1, 1, -1, -1],
[7, -1, -1, 3, -1, 8, -1, -1, 2],
[-1, -1, 3, -1, 6, 2, -1, -1, 7],
[-1, 5, -1, 7, -1, 9, 2, 1, -1],
[-1, 6, 4, -1, -1, -1, 9, -1, -1],
[-1, -1, -1, 2, -1, -1, 4, 3, 8]
];
const expected1 = [
[8, 1, 9, 6, 7, 5, 3, 2, 4],
[6, 4, 2, 9, 8, 3, 7, 5, 1],
[5, 3, 7, 1, 2, 4, 8, 6, 9],
[4, 2, 6, 5, 9, 7, 1, 8, 3],
[7, 9, 5, 3, 1, 8, 6, 4, 2],
[1, 8, 3, 4, 6, 2, 5, 9, 7],
[3, 5, 8, 7, 4, 9, 2, 1, 6],
[2, 6, 4, 8, 3, 1, 9, 7, 5],
[9, 7, 1, 2, 5, 6, 4, 3, 8]
];
const puzzle2 = [
[5, 3, -1, -1, 2, 4, 7, -1, -1],
[-1, -1, 2, -1, -1, -1, 8, -1, -1],
[1, -1, -1, 7, -1, 3, 9, -1, 2],
[-1, -1, 8, -1, 7, 2, -1, 4, 9],
[-1, 2, -1, 9, 8, -1, -1, 7, -1],
[7, 9, -1, -1, -1, -1, -1, 8, -1],
[-1, -1, -1, -1, 3, -1, 5, -1, 6],
[9, 6, -1, -1, 1, -1, 3, -1, -1],
[-1, 5, -1, 6, 9, -1, -1, 1, -1]
];
const expected2 = [
[5, 3, 9, 8, 2, 4, 7, 6, 1],
[6, 7, 2, 1, 5, 9, 8, 3, 4],
[1, 8, 4, 7, 6, 3, 9, 5, 2],
[3, 1, 8, 5, 7, 2, 6, 4, 9],
[4, 2, 5, 9, 8, 6, 1, 7, 3],
[7, 9, 6, 3, 4, 1, 2, 8, 5],
[8, 4, 1, 2, 3, 7, 5, 9, 6],
[9, 6, 7, 4, 1, 5, 3, 2, 8],
[2, 5, 3, 6, 9, 8, 4, 1, 7]
];
const puzzle3 = [
[-1, -1, 3, -1, 2, -1, 6, -1, -1],
[9, -1, -1, 3, -1, 5, -1, -1, 1],
[-1, -1, 1, 8, -1, 6, 4, -1, -1],
[-1, -1, 8, 1, -1, 2, 9, -1, -1],
[7, -1, -1, -1, -1, -1, -1, -1, 8],
[-1, -1, 6, 7, -1, 8, 2, -1, -1],
[-1, -1, 2, 6, -1, 9, 5, -1, -1],
[8, -1, -1, 2, -1, 3, -1, -1, 9],
[-1, -1, 5, -1, 1, -1, 3, -1, -1]
];
const expected3 = [
[4, 8, 3, 9, 2, 1, 6, 5, 7],
[9, 6, 7, 3, 4, 5, 8, 2, 1],
[2, 5, 1, 8, 7, 6, 4, 9, 3],
[5, 4, 8, 1, 3, 2, 9, 7, 6],
[7, 2, 9, 5, 6, 4, 1, 3, 8],
[1, 3, 6, 7, 9, 8, 2, 4, 5],
[3, 7, 2, 6, 8, 9, 5, 1, 4],
[8, 1, 4, 2, 5, 3, 7, 6, 9],
[6, 9, 5, 4, 1, 7, 3, 8, 2]
];
```
# --hints--
`solveSudoku` should be a function.
@@ -21,108 +94,25 @@ assert(typeof solveSudoku == 'function');
`solveSudoku([[8, 1, 9, -1, -1, 5, -1, -1, -1],[-1, -1, 2, -1, -1, -1, 7, 5, -1],[-1, 3, 7, 1, -1, 4, -1, 6, -1],[4, -1, -1, 5, 9, -1, 1, -1, -1],[7, -1, -1, 3, -1, 8, -1, -1, 2],[-1, -1, 3, -1, 6, 2, -1, -1, 7],[-1, 5, -1, 7, -1, 9, 2, 1, -1],[-1, 6, 4, -1, -1, -1, 9, -1, -1],[-1, -1, -1, 2, -1, -1, 4, 3, 8]])` should return an array.
```js
assert(
Array.isArray(
solveSudoku([
[8, 1, 9, -1, -1, 5, -1, -1, -1],
[-1, -1, 2, -1, -1, -1, 7, 5, -1],
[-1, 3, 7, 1, -1, 4, -1, 6, -1],
[4, -1, -1, 5, 9, -1, 1, -1, -1],
[7, -1, -1, 3, -1, 8, -1, -1, 2],
[-1, -1, 3, -1, 6, 2, -1, -1, 7],
[-1, 5, -1, 7, -1, 9, 2, 1, -1],
[-1, 6, 4, -1, -1, -1, 9, -1, -1],
[-1, -1, -1, 2, -1, -1, 4, 3, 8]
])
)
);
assert(Array.isArray(solveSudoku(puzzle1)));
```
`solveSudoku([[8, 1, 9, -1, -1, 5, -1, -1, -1],[-1, -1, 2, -1, -1, -1, 7, 5, -1],[-1, 3, 7, 1, -1, 4, -1, 6, -1],[4, -1, -1, 5, 9, -1, 1, -1, -1],[7, -1, -1, 3, -1, 8, -1, -1, 2],[-1, -1, 3, -1, 6, 2, -1, -1, 7],[-1, 5, -1, 7, -1, 9, 2, 1, -1],[-1, 6, 4, -1, -1, -1, 9, -1, -1],[-1, -1, -1, 2, -1, -1, 4, 3, 8]])` should return `[[8, 1, 9, 6, 7, 5, 3, 2, 4],[6, 4, 2, 9, 8, 3, 7, 5, 1],[5, 3, 7, 1, 2, 4, 8, 6, 9],[4, 2, 6, 5, 9, 7, 1, 8, 3],[7, 9, 5, 3, 1, 8, 6, 4, 2],[1, 8, 3, 4, 6, 2, 5, 9, 7],[3, 5, 8, 7, 4, 9, 2, 1, 6],[2, 6, 4, 8, 3, 1, 9, 7, 5],[9, 7, 1, 2, 5, 6, 4, 3, 8]]`.
```js
assert.deepEqual(
solveSudoku([
[8, 1, 9, -1, -1, 5, -1, -1, -1],
[-1, -1, 2, -1, -1, -1, 7, 5, -1],
[-1, 3, 7, 1, -1, 4, -1, 6, -1],
[4, -1, -1, 5, 9, -1, 1, -1, -1],
[7, -1, -1, 3, -1, 8, -1, -1, 2],
[-1, -1, 3, -1, 6, 2, -1, -1, 7],
[-1, 5, -1, 7, -1, 9, 2, 1, -1],
[-1, 6, 4, -1, -1, -1, 9, -1, -1],
[-1, -1, -1, 2, -1, -1, 4, 3, 8]
]),
[
[8, 1, 9, 6, 7, 5, 3, 2, 4],
[6, 4, 2, 9, 8, 3, 7, 5, 1],
[5, 3, 7, 1, 2, 4, 8, 6, 9],
[4, 2, 6, 5, 9, 7, 1, 8, 3],
[7, 9, 5, 3, 1, 8, 6, 4, 2],
[1, 8, 3, 4, 6, 2, 5, 9, 7],
[3, 5, 8, 7, 4, 9, 2, 1, 6],
[2, 6, 4, 8, 3, 1, 9, 7, 5],
[9, 7, 1, 2, 5, 6, 4, 3, 8]
]
);
assert.deepEqual(solveSudoku(puzzle1), expected1);
```
`solveSudoku([[5, 3, -1, -1, 2, 4, 7, -1, -1],[-1, -1, 2, -1, -1, -1, 8, -1, -1],[1, -1, -1, 7, -1, 3, 9, -1, 2],[-1, -1, 8, -1, 7, 2, -1, 4, 9],[-1, 2, -1, 9, 8, -1, -1, 7, -1],[7, 9, -1, -1, -1, -1, -1, 8, -1],[-1, -1, -1, -1, 3, -1, 5, -1, 6],[9, 6, -1, -1, 1, -1, 3, -1, -1],[-1, 5, -1, 6, 9, -1, -1, 1, -1]])` should return `[[5, 3, 9, 8, 2, 4, 7, 6, 1],[6, 7, 2, 1, 5, 9, 8, 3, 4],[1, 8, 4, 7, 6, 3, 9, 5, 2],[3, 1, 8, 5, 7, 2, 6, 4, 9],[4, 2, 5, 9, 8, 6, 1, 7, 3],[7, 9, 6, 3, 4, 1, 2, 8, 5],[8, 4, 1, 2, 3, 7, 5, 9, 6],[9, 6, 7, 4, 1, 5, 3, 2, 8],[2, 5, 3, 6, 9, 8, 4, 1, 7]]`.
```js
assert.deepEqual(
solveSudoku([
[5, 3, -1, -1, 2, 4, 7, -1, -1],
[-1, -1, 2, -1, -1, -1, 8, -1, -1],
[1, -1, -1, 7, -1, 3, 9, -1, 2],
[-1, -1, 8, -1, 7, 2, -1, 4, 9],
[-1, 2, -1, 9, 8, -1, -1, 7, -1],
[7, 9, -1, -1, -1, -1, -1, 8, -1],
[-1, -1, -1, -1, 3, -1, 5, -1, 6],
[9, 6, -1, -1, 1, -1, 3, -1, -1],
[-1, 5, -1, 6, 9, -1, -1, 1, -1]
]),
[
[5, 3, 9, 8, 2, 4, 7, 6, 1],
[6, 7, 2, 1, 5, 9, 8, 3, 4],
[1, 8, 4, 7, 6, 3, 9, 5, 2],
[3, 1, 8, 5, 7, 2, 6, 4, 9],
[4, 2, 5, 9, 8, 6, 1, 7, 3],
[7, 9, 6, 3, 4, 1, 2, 8, 5],
[8, 4, 1, 2, 3, 7, 5, 9, 6],
[9, 6, 7, 4, 1, 5, 3, 2, 8],
[2, 5, 3, 6, 9, 8, 4, 1, 7]
]
);
assert.deepEqual(solveSudoku(puzzle2), expected2);
```
`solveSudoku([[-1, -1, 3, -1, 2, -1, 6, -1, -1],[9, -1, -1, 3, -1, 5, -1, -1, 1],[-1, -1, 1, 8, -1, 6, 4, -1, -1],[-1, -1, 8, 1, -1, 2, 9, -1, -1],[7, -1, -1, -1, -1, -1, -1, -1, 8],[-1, -1, 6, 7, -1, 8, 2, -1, -1],[-1, -1, 2, 6, -1, 9, 5, -1, -1],[8, -1, -1, 2, -1, 3, -1, -1, 9],[-1, -1, 5, -1, 1, -1, 3, -1, -1]])` should return `[[4, 8, 3, 9, 2, 1, 6, 5, 7],[9, 6, 7, 3, 4, 5, 8, 2, 1],[2, 5, 1, 8, 7, 6, 4, 9, 3],[5, 4, 8, 1, 3, 2, 9, 7, 6],[7, 2, 9, 5, 6, 4, 1, 3, 8],[1, 3, 6, 7, 9, 8, 2, 4, 5],[3, 7, 2, 6, 8, 9, 5, 1, 4],[8, 1, 4, 2, 5, 3, 7, 6, 9],[6, 9, 5, 4, 1, 7, 3, 8, 2]]`.
```js
assert.deepEqual(
solveSudoku([
[-1, -1, 3, -1, 2, -1, 6, -1, -1],
[9, -1, -1, 3, -1, 5, -1, -1, 1],
[-1, -1, 1, 8, -1, 6, 4, -1, -1],
[-1, -1, 8, 1, -1, 2, 9, -1, -1],
[7, -1, -1, -1, -1, -1, -1, -1, 8],
[-1, -1, 6, 7, -1, 8, 2, -1, -1],
[-1, -1, 2, 6, -1, 9, 5, -1, -1],
[8, -1, -1, 2, -1, 3, -1, -1, 9],
[-1, -1, 5, -1, 1, -1, 3, -1, -1]
]),
[
[4, 8, 3, 9, 2, 1, 6, 5, 7],
[9, 6, 7, 3, 4, 5, 8, 2, 1],
[2, 5, 1, 8, 7, 6, 4, 9, 3],
[5, 4, 8, 1, 3, 2, 9, 7, 6],
[7, 2, 9, 5, 6, 4, 1, 3, 8],
[1, 3, 6, 7, 9, 8, 2, 4, 5],
[3, 7, 2, 6, 8, 9, 5, 1, 4],
[8, 1, 4, 2, 5, 3, 7, 6, 9],
[6, 9, 5, 4, 1, 7, 3, 8, 2]
]
);
assert.deepEqual(solveSudoku(puzzle3), expected3);
```
# --seed--

View File

@@ -20,6 +20,75 @@ and clip it by the rectangle defined by the points:
Write a function that takes 2 arrays as parameters. The first array contains the points of the subject polygon and the second array contains the points of the clipping polygon. The function should return an array containing the points of the clipped polygon. Each number should be rounded to 3 decimal places.
# --before-each--
```js
const subjectPolygon1 = [
[50, 150],
[200, 50],
[350, 150],
[350, 300],
[250, 300],
[200, 250],
[150, 350],
[100, 250],
[100, 200]
];
const clipPolygon1 = [
[100, 100],
[300, 100],
[300, 300],
[100, 300]
];
const expected1 = [
[100, 116.667],
[125, 100],
[275, 100],
[300, 116.667],
[300, 300],
[250, 300],
[200, 250],
[175, 300],
[125, 300],
[100, 250]
];
const subjectPolygon2 = [
[150, 200],
[400, 450],
[30, 50]
];
const clipPolygon2 = [
[10, 10],
[300, 200],
[400, 600],
[100, 300]
];
const expected2 = [
[150, 200],
[350, 400],
[348.611, 394.444],
[30, 50]
];
const subjectPolygon3 = [
[250, 200],
[100, 450],
[130, 250]
];
const clipPolygon3 = [
[50, 60],
[100, 230],
[400, 600],
[100, 300]
];
const expected3 = [
[129.167, 329.167],
[119.565, 319.565],
[121.854, 304.305]
];
```
# --hints--
`clip` should be a function.
@@ -31,118 +100,25 @@ assert(typeof clip == 'function');
`clip([[50, 150], [200, 50], [350, 150], [350, 300], [250, 300], [200, 250], [150, 350], [100, 250], [100, 200]], [[100, 100], [300, 100], [300, 300], [100, 300]])` should return an array.
```js
assert(
Array.isArray(
clip(
[
[50, 150],
[200, 50],
[350, 150],
[350, 300],
[250, 300],
[200, 250],
[150, 350],
[100, 250],
[100, 200]
],
[
[100, 100],
[300, 100],
[300, 300],
[100, 300]
]
)
)
);
assert(Array.isArray(clip(subjectPolygon1, clipPolygon1)));
```
`clip([[50, 150], [200, 50], [350, 150], [350, 300], [250, 300], [200, 250], [150, 350], [100, 250], [100, 200]], [[100, 100], [300, 100], [300, 300], [100, 300]])` should return `[[100, 116.667], [125, 100], [275, 100], [300, 116.667], [300, 300], [250, 300], [200, 250], [175, 300], [125, 300], [100, 250]]`.
```js
assert.deepEqual(
clip(
[
[50, 150],
[200, 50],
[350, 150],
[350, 300],
[250, 300],
[200, 250],
[150, 350],
[100, 250],
[100, 200]
],
[
[100, 100],
[300, 100],
[300, 300],
[100, 300]
]
),
[
[100, 116.667],
[125, 100],
[275, 100],
[300, 116.667],
[300, 300],
[250, 300],
[200, 250],
[175, 300],
[125, 300],
[100, 250]
]
);
assert.deepEqual(clip(subjectPolygon1, clipPolygon1), expected1);
```
`clip([[150, 200], [400, 450], [30, 50]], [[10, 10], [300, 200], [400, 600], [100, 300]])` should return `[[150, 200], [350, 400], [348.611, 394.444], [30, 50]]`.
```js
assert.deepEqual(
clip(
[
[150, 200],
[400, 450],
[30, 50]
],
[
[10, 10],
[300, 200],
[400, 600],
[100, 300]
]
),
[
[150, 200],
[350, 400],
[348.611, 394.444],
[30, 50]
]
);
assert.deepEqual(clip(subjectPolygon2, clipPolygon2), expected2);
```
`clip([[250, 200], [100, 450], [130, 250]], [[50, 60], [100, 230], [400, 600], [100, 300]])` should return `[[129.167, 329.167], [119.565, 319.565], [121.854, 304.305]]`.
```js
assert.deepEqual(
clip(
[
[250, 200],
[100, 450],
[130, 250]
],
[
[50, 60],
[100, 230],
[400, 600],
[100, 300]
]
),
[
[129.167, 329.167],
[119.565, 319.565],
[121.854, 304.305]
]
);
assert.deepEqual(clip(subjectPolygon3, clipPolygon3), expected3);
```
# --seed--

View File

@@ -82,6 +82,106 @@ $PA = LU$
The task is to implement a routine which will take a square nxn matrix $A$ and return a lower triangular matrix $L$, a upper triangular matrix $U$ and a permutation matrix $P$, so that the above equation is fulfilled. The returned value should be in the form `[L, U, P]`.
# --before-each--
```js
const matrix1 = [
[1, 3, 5],
[2, 4, 7],
[1, 1, 0]
];
const expected1 = [
[
[1, 0, 0],
[0.5, 1, 0],
[0.5, -1, 1]
],
[
[2, 4, 7],
[0, 1, 1.5],
[0, 0, -2]
],
[
[0, 1, 0],
[1, 0, 0],
[0, 0, 1]
]
];
const matrix2 = [
[11, 9, 24, 2],
[1, 5, 2, 6],
[3, 17, 18, 1],
[2, 5, 7, 1]
];
const expected2 = [
[
[1, 0, 0, 0],
[0.2727272727272727, 1, 0, 0],
[0.09090909090909091, 0.2875, 1, 0],
[0.18181818181818182, 0.23124999999999996, 0.0035971223021580693, 1]
],
[
[11, 9, 24, 2],
[0, 14.545454545454547, 11.454545454545455, 0.4545454545454546],
[0, 0, -3.4749999999999996, 5.6875],
[0, 0, 0, 0.510791366906476]
],
[
[1, 0, 0, 0],
[0, 0, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 1]
]
];
const matrix3 = [
[1, 1, 1],
[4, 3, -1],
[3, 5, 3]
];
const expected3 = [
[
[1, 0, 0],
[0.75, 1, 0],
[0.25, 0.09090909090909091, 1]
],
[
[4, 3, -1],
[0, 2.75, 3.75],
[0, 0, 0.9090909090909091]
],
[
[0, 1, 0],
[0, 0, 1],
[1, 0, 0]
]
];
const matrix4 = [
[1, -2, 3],
[2, -5, 12],
[0, 2, -10]
];
const expected4 = [
[
[1, 0, 0],
[0, 1, 0],
[0.5, 0.25, 1]
],
[
[2, -5, 12],
[0, 2, -10],
[0, 0, -0.5]
],
[
[0, 1, 0],
[0, 0, 1],
[1, 0, 0]
]
];
```
# --hints--
`luDecomposition` should be a function.
@@ -93,135 +193,31 @@ assert(typeof luDecomposition == 'function');
`luDecomposition([[1, 3, 5], [2, 4, 7], [1, 1, 0]])` should return a array.
```js
assert(
Array.isArray(
luDecomposition([
[1, 3, 5],
[2, 4, 7],
[1, 1, 0]
])
)
);
assert(Array.isArray(luDecomposition(matrix1)));
```
`luDecomposition([[1, 3, 5], [2, 4, 7], [1, 1, 0]])` should return `[[[1, 0, 0], [0.5, 1, 0], [0.5, -1, 1]], [[2, 4, 7], [0, 1, 1.5], [0, 0, -2]], [[0, 1, 0], [1, 0, 0], [0, 0, 1]]]`.
```js
assert.deepEqual(
luDecomposition([
[1, 3, 5],
[2, 4, 7],
[1, 1, 0]
]),
[
[
[1, 0, 0],
[0.5, 1, 0],
[0.5, -1, 1]
],
[
[2, 4, 7],
[0, 1, 1.5],
[0, 0, -2]
],
[
[0, 1, 0],
[1, 0, 0],
[0, 0, 1]
]
]
);
assert.deepEqual(luDecomposition(matrix1), expected1);
```
`luDecomposition([[11, 9, 24, 2], [1, 5, 2, 6], [3, 17, 18, 1], [2, 5, 7, 1]])` should return `[[[1, 0, 0, 0], [0.2727272727272727, 1, 0, 0], [0.09090909090909091, 0.2875, 1, 0], [0.18181818181818182, 0.23124999999999996, 0.0035971223021580693, 1]], [[11, 9, 24, 2], [0, 14.545454545454547, 11.454545454545455, 0.4545454545454546], [0, 0, -3.4749999999999996, 5.6875], [0, 0, 0, 0.510791366906476]], [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]]`.
```js
assert.deepEqual(
luDecomposition([
[11, 9, 24, 2],
[1, 5, 2, 6],
[3, 17, 18, 1],
[2, 5, 7, 1]
]),
[
[
[1, 0, 0, 0],
[0.2727272727272727, 1, 0, 0],
[0.09090909090909091, 0.2875, 1, 0],
[0.18181818181818182, 0.23124999999999996, 0.0035971223021580693, 1]
],
[
[11, 9, 24, 2],
[0, 14.545454545454547, 11.454545454545455, 0.4545454545454546],
[0, 0, -3.4749999999999996, 5.6875],
[0, 0, 0, 0.510791366906476]
],
[
[1, 0, 0, 0],
[0, 0, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 1]
]
]
);
assert.deepEqual(luDecomposition(matrix2), expected2);
```
`luDecomposition([[1, 1, 1], [4, 3, -1], [3, 5, 3]])` should return `[[[1, 0, 0], [0.75, 1, 0], [0.25, 0.09090909090909091, 1]], [[4, 3, -1], [0, 2.75, 3.75], [0, 0, 0.9090909090909091]], [[0, 1, 0], [0, 0, 1], [1, 0, 0]]]`.
```js
assert.deepEqual(
luDecomposition([
[1, 1, 1],
[4, 3, -1],
[3, 5, 3]
]),
[
[
[1, 0, 0],
[0.75, 1, 0],
[0.25, 0.09090909090909091, 1]
],
[
[4, 3, -1],
[0, 2.75, 3.75],
[0, 0, 0.9090909090909091]
],
[
[0, 1, 0],
[0, 0, 1],
[1, 0, 0]
]
]
);
assert.deepEqual(luDecomposition(matrix3), expected3);
```
`luDecomposition([[1, -2, 3], [2, -5, 12], [0, 2, -10]])` should return `[[[1, 0, 0], [0, 1, 0], [0.5, 0.25, 1]], [[2, -5, 12], [0, 2, -10], [0, 0, -0.5]], [[0, 1, 0], [0, 0, 1], [1, 0, 0]]]`.
```js
assert.deepEqual(
luDecomposition([
[1, -2, 3],
[2, -5, 12],
[0, 2, -10]
]),
[
[
[1, 0, 0],
[0, 1, 0],
[0.5, 0.25, 1]
],
[
[2, -5, 12],
[0, 2, -10],
[0, 0, -0.5]
],
[
[0, 1, 0],
[0, 0, 1],
[1, 0, 0]
]
]
);
assert.deepEqual(luDecomposition(matrix4), expected4);
```
# --seed--