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: a3f503de51cf954ede28891d
|
||||
title: 找到对称差异
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: find-the-symmetric-difference
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -115,5 +116,29 @@ assert.equal(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sym(args) {
|
||||
return args;
|
||||
}
|
||||
|
||||
sym([1, 2, 3], [5, 2, 1, 4]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function sym() {
|
||||
var arrays = [].slice.call(arguments);
|
||||
return arrays.reduce(function (symDiff, arr) {
|
||||
return symDiff.concat(arr).filter(function (val, idx, theArr) {
|
||||
return theArr.indexOf(val) === idx
|
||||
&& (symDiff.indexOf(val) === -1 || arr.indexOf(val) === -1);
|
||||
});
|
||||
});
|
||||
}
|
||||
sym([1, 2, 3], [5, 2, 1, 4]);
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 8d5123c8c441eddfaeb5bdef
|
||||
title: 实施冒泡排序
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: implement-bubble-sort
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -79,5 +80,56 @@ assert.sameMembers(
|
||||
assert.strictEqual(code.search(/\.sort\(/), -1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isBuiltInSortUsed(){
|
||||
let sortUsed = false;
|
||||
Array.prototype.sort = () => sortUsed = true;
|
||||
bubbleSort([0, 1]);
|
||||
return !sortUsed;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function bubbleSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
bubbleSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function bubbleSort(array) {
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
let swapped = false;
|
||||
for (let j = 1; j < array.length; j++) {
|
||||
if (array[j - 1] > array[j]) {
|
||||
let temp = array[j-1];
|
||||
array[j-1] = array[j];
|
||||
array[j] = temp;
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
if (swapped === false) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8259367417b2b2512c86
|
||||
title: 实现插入排序
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: implement-insertion-sort
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -79,5 +80,51 @@ assert.sameMembers(
|
||||
assert.strictEqual(code.search(/\.sort\(/), -1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isBuiltInSortUsed(){
|
||||
let sortUsed = false;
|
||||
Array.prototype.sort = () => sortUsed = true;
|
||||
insertionSort([0, 1]);
|
||||
return !sortUsed;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function insertionSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
insertionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function insertionSort (array) {
|
||||
for (let currentIndex = 0; currentIndex < array.length; currentIndex++) {
|
||||
let current = array[currentIndex];
|
||||
let j = currentIndex - 1;
|
||||
while (j > -1 && array[j] > current) {
|
||||
array[j + 1] = array[j];
|
||||
j--;
|
||||
}
|
||||
array[j + 1] = current;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d825c367417b2b2512c8f
|
||||
title: 实现合并排序
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: implement-merge-sort
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -79,5 +80,70 @@ assert.sameMembers(
|
||||
assert.strictEqual(code.search(/\.sort\(/), -1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isBuiltInSortUsed(){
|
||||
let sortUsed = false;
|
||||
Array.prototype.sort = () => sortUsed = true;
|
||||
mergeSort([0, 1]);
|
||||
return !sortUsed;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
if (array.length === 1) {
|
||||
return array;
|
||||
} else {
|
||||
const splitIndex = Math.floor(array.length / 2);
|
||||
return merge(
|
||||
mergeSort(array.slice(0, splitIndex)),
|
||||
mergeSort(array.slice(splitIndex))
|
||||
);
|
||||
}
|
||||
|
||||
// Merge two sorted arrays
|
||||
function merge(array1, array2) {
|
||||
let merged = [];
|
||||
while (array1.length && array2.length) {
|
||||
if (array1[0] < array2[0]) {
|
||||
merged.push(array1.shift());
|
||||
} else if (array1[0] > array2[0]) {
|
||||
merged.push(array2.shift());
|
||||
} else {
|
||||
merged.push(array1.shift(), array2.shift());
|
||||
}
|
||||
}
|
||||
|
||||
// After looping ends, one array is empty, and other array contains only
|
||||
// values greater than all values in `merged`
|
||||
return [...merged, ...array1, ...array2];
|
||||
}
|
||||
}
|
||||
|
||||
mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d825a367417b2b2512c89
|
||||
title: 实施快速排序
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: implement-quick-sort
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -79,5 +80,60 @@ assert.sameMembers(
|
||||
assert.strictEqual(code.search(/\.sort\(/), -1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isBuiltInSortUsed(){
|
||||
let sortUsed = false;
|
||||
Array.prototype.sort = () => sortUsed = true;
|
||||
quickSort([0, 1]);
|
||||
return !sortUsed;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function quickSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function quickSort(array) {
|
||||
if (array.length === 0) {
|
||||
return [];
|
||||
} else {
|
||||
const pivotValue = array[0];
|
||||
|
||||
// Sort elements into three piles
|
||||
let lesser = [];
|
||||
let equal = [];
|
||||
let greater = [];
|
||||
for (let e of array) {
|
||||
if (e < pivotValue) {
|
||||
lesser.push(e);
|
||||
} else if (e > pivotValue) {
|
||||
greater.push(e);
|
||||
} else {
|
||||
equal.push(e);
|
||||
}
|
||||
}
|
||||
|
||||
return [...quickSort(lesser), ...equal, ...quickSort(greater)];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: 587d8259367417b2b2512c85
|
||||
title: 实施选择排序
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
dashedName: implement-selection-sort
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -79,5 +80,54 @@ assert.sameMembers(
|
||||
assert.strictEqual(code.search(/\.sort\(/), -1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
for(let i = 0; i < a.length - 1; i++)
|
||||
if(a[i] > a[i + 1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function isBuiltInSortUsed(){
|
||||
let sortUsed = false;
|
||||
Array.prototype.sort = () => sortUsed = true;
|
||||
selectionSort([0, 1]);
|
||||
return !sortUsed;
|
||||
}
|
||||
```
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function selectionSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
|
||||
selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function selectionSort(array) {
|
||||
for (let i = 0; i < array.length-1; i++) {
|
||||
let minimumIndex = i;
|
||||
for (let j = i+1; j < array.length; j++){
|
||||
if (array[j] < array[minimumIndex]) {
|
||||
minimumIndex = j;
|
||||
}
|
||||
}
|
||||
let value = array[minimumIndex];
|
||||
array[minimumIndex] = array[i];
|
||||
array[i] = value;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: a56138aff60341a09ed6c480
|
||||
title: 库存更新
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: inventory-update
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -156,5 +157,72 @@ assert.deepEqual(
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function updateInventory(arr1, arr2) {
|
||||
return arr1;
|
||||
}
|
||||
|
||||
// Example inventory lists
|
||||
var curInv = [
|
||||
[21, "Bowling Ball"],
|
||||
[2, "Dirty Sock"],
|
||||
[1, "Hair Pin"],
|
||||
[5, "Microphone"]
|
||||
];
|
||||
|
||||
var newInv = [
|
||||
[2, "Hair Pin"],
|
||||
[3, "Half-Eaten Apple"],
|
||||
[67, "Bowling Ball"],
|
||||
[7, "Toothpaste"]
|
||||
];
|
||||
|
||||
updateInventory(curInv, newInv);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function updateInventory(arr1, arr2) {
|
||||
arr2.forEach(function(item) {
|
||||
createOrUpdate(arr1, item);
|
||||
});
|
||||
// All inventory must be accounted for or you're fired!
|
||||
return arr1;
|
||||
}
|
||||
|
||||
function createOrUpdate(arr1, item) {
|
||||
var index = -1;
|
||||
while (++index < arr1.length) {
|
||||
if (arr1[index][1] === item[1]) {
|
||||
arr1[index][0] += item[0];
|
||||
return;
|
||||
}
|
||||
if (arr1[index][1] > item[1]) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
arr1.splice(index, 0, item);
|
||||
}
|
||||
|
||||
// Example inventory lists
|
||||
var curInv = [
|
||||
[21, 'Bowling Ball'],
|
||||
[2, 'Dirty Sock'],
|
||||
[1, 'Hair Pin'],
|
||||
[5, 'Microphone']
|
||||
];
|
||||
|
||||
var newInv = [
|
||||
[2, 'Hair Pin'],
|
||||
[3, 'Half-Eaten Apple'],
|
||||
[67, 'Bowling Ball'],
|
||||
[7, 'Toothpaste']
|
||||
];
|
||||
|
||||
updateInventory(curInv, newInv);
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: a7bf700cd123b9a54eef01d5
|
||||
title: 请不要重复
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: no-repeats-please
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -71,5 +72,53 @@ assert.strictEqual(permAlone('aaab'), 0);
|
||||
assert.strictEqual(permAlone('aaabb'), 12);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function permAlone(str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
permAlone('aab');
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function permAlone(str) {
|
||||
return permuter(str).filter(function(perm) {
|
||||
return !perm.match(/(.)\1/g);
|
||||
}).length;
|
||||
}
|
||||
|
||||
function permuter(str) {
|
||||
// http://staff.roguecc.edu/JMiller/JavaScript/permute.html
|
||||
//permArr: Global array which holds the list of permutations
|
||||
//usedChars: Global utility array which holds a list of "currently-in-use" characters
|
||||
var permArr = [], usedChars = [];
|
||||
function permute(input) {
|
||||
//convert input into a char array (one element for each character)
|
||||
var i, ch, chars = input.split("");
|
||||
for (i = 0; i < chars.length; i++) {
|
||||
//get and remove character at index "i" from char array
|
||||
ch = chars.splice(i, 1);
|
||||
//add removed character to the end of used characters
|
||||
usedChars.push(ch);
|
||||
//when there are no more characters left in char array to add, add used chars to list of permutations
|
||||
if (chars.length === 0) permArr[permArr.length] = usedChars.join("");
|
||||
//send characters (minus the removed one from above) from char array to be permuted
|
||||
permute(chars.join(""));
|
||||
//add removed character back into char array in original position
|
||||
chars.splice(i, 0, ch);
|
||||
//remove the last character used off the end of used characters array
|
||||
usedChars.pop();
|
||||
}
|
||||
}
|
||||
permute(str);
|
||||
return permArr;
|
||||
}
|
||||
|
||||
permAlone('aab');
|
||||
```
|
||||
|
@ -3,6 +3,7 @@ id: a3f503de51cfab748ff001aa
|
||||
title: 成对
|
||||
challengeType: 5
|
||||
videoUrl: ''
|
||||
dashedName: pairwise
|
||||
---
|
||||
|
||||
# --description--
|
||||
@ -49,5 +50,37 @@ assert.deepEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10);
|
||||
assert.deepEqual(pairwise([], 100), 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function pairwise(arr, arg) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
pairwise([1,4,2,3,0,5], 7);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function pairwise(arr, arg) {
|
||||
var sum = 0;
|
||||
arr.forEach(function(e, i, a) {
|
||||
if (e != null) {
|
||||
var diff = arg-e;
|
||||
a[i] = null;
|
||||
var dix = a.indexOf(diff);
|
||||
if (dix !== -1) {
|
||||
sum += dix;
|
||||
sum += i;
|
||||
a[dix] = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
return sum;
|
||||
}
|
||||
|
||||
pairwise([1,4,2,3,0,5], 7);
|
||||
```
|
||||
|
Reference in New Issue
Block a user