Skip to content

Commit

Permalink
feat: 新增 sort 题目
Browse files Browse the repository at this point in the history
  • Loading branch information
xjq committed Nov 16, 2022
1 parent a366ce0 commit 0ea1b42
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 32 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"release": "standard-version",
"generate-qs": "node qs.mjs"
"qs:generate": "node qs.mjs",
"qs:create": "node qscreate.mjs"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion qs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const root = path.resolve(process.cwd(), 'question/FrontEnd');

for (const dir of dirs) {
const stat = await fs.lstat(path.resolve(root, dir));
if (stat.isDirectory()) {
if (stat.isDirectory() && dir !== 'template') {
const files = await fs.readdir(path.resolve(root, dir));
json[dir] = {};
for (const file of files) {
Expand Down
16 changes: 16 additions & 0 deletions qscreate.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import fs from 'fs/promises';
import path from 'path';

const [name] = process.argv.slice(2);

const root = path.resolve(process.cwd(), 'question/FrontEnd');

(async function () {
const templateDir = root + '/template/';
const createDir = root + '/' + name + '/';
const files = await fs.readdir(templateDir);
await fs.mkdir(createDir);
for (const file of files) {
await fs.copyFile(templateDir + file, createDir + file);
}
})();
4 changes: 4 additions & 0 deletions question/FrontEnd/sort/answer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function f(arr) {
// js api
return arr.sort();
}
41 changes: 41 additions & 0 deletions question/FrontEnd/sort/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## Sort

将数组按从小到大的顺序排序

输入为 数字数组

- 冒泡排序
- 选择排序
- 插入排序
- 归并排序
- 堆排序
- 快速排序

...

用例 1:

```js
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const sortArr = sort(arr);

console.log(sortArr); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
```

用例 2:

```js
const arr = [1, 9, 2, 4, 3, 7, 6, 8, 6];
const sortArr = sort(arr);

console.log(sortArr); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
```

用例 3:

```js
const arr = [1, 1, 1, 3, 4, 5, 7, 6, 6, 6, 0, 0, -1];
const sortArr = sort(arr);

console.log(sortArr); // [-1, 0, 0, 1, 1, 1, 3, 4, 5, 6, 6, 6, 7]
```
3 changes: 3 additions & 0 deletions question/FrontEnd/sort/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function f(arr) {
return [];
}
47 changes: 47 additions & 0 deletions question/FrontEnd/sort/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import assert from 'assert';
import f from './index.mjs';

const cases = [
{
Input: [],
Expected: [],
Message: '空数组',
},
{
Input: [1, 2, 3, 4, 5, 6, 7, 8, 9],
Expected: [1, 2, 3, 4, 5, 6, 7, 8, 9],
Message: '已排序好的数组',
},
{
Input: [1, 9, 2, 4, 3, 7, 6, 8, 5],
Expected: [1, 2, 3, 4, 5, 6, 7, 8, 9],
Message: '乱序数组一',
},
{
Input: [1, 1, 1, 3, 4, 5, 7, 6, 6, 6, 0, 0, -1],
Expected: [-1, 0, 0, 1, 1, 1, 3, 4, 5, 6, 6, 6, 7],
Message: '乱序数组二-包含负数',
},
];

(function () {
for (let i = 0; i < cases.length; i++) {
const { Input, Expected, Message } = cases[i];
let output;
try {
output = f(Input);
assert.deepEqual(output, Expected);
} catch (error) {
console.log('用例 ' + String(i + 1) + ': ' + Message + ' 未通过');
if (error.code === 'ERR_ASSERTION') {
console.log('Input:', JSON.stringify(Input));
console.log('Expected:', JSON.stringify(error.expected));
console.log('Received:', JSON.stringify(output));
} else {
console.log(error);
}
break;
}
console.log('用例 ' + String(i + 1) + ': ' + Message + ' 通过');
}
})();
3 changes: 3 additions & 0 deletions question/FrontEnd/template/answer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function f(arr) {
return arr;
}
15 changes: 15 additions & 0 deletions question/FrontEnd/template/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
##

...

用例 1:

```js

```

用例 2:

```js

```
3 changes: 3 additions & 0 deletions question/FrontEnd/template/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function f(arr) {
return [];
}
32 changes: 32 additions & 0 deletions question/FrontEnd/template/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import assert from 'assert';
import f from './index.mjs';

const cases = [
{
Input: [1, 2, 3],
Expected: [1, 2, 3],
Message: '...',
},
];

(function () {
for (let i = 0; i < cases.length; i++) {
const { Input, Expected, Message } = cases[i];
let output;
try {
output = f(Input);
assert.deepEqual(output, Expected);
} catch (error) {
console.log('用例 ' + String(i + 1) + ': ' + Message + ' 未通过');
if (error.code === 'ERR_ASSERTION') {
console.log('Input:', JSON.stringify(Input));
console.log('Expected:', JSON.stringify(error.expected));
console.log('Received:', JSON.stringify(output));
} else {
console.log(error);
}
break;
}
console.log('用例 ' + String(i + 1) + ': ' + Message + ' 通过');
}
})();
31 changes: 1 addition & 30 deletions server/src/qsdata/questions.json

Large diffs are not rendered by default.

0 comments on commit 0ea1b42

Please sign in to comment.