Skip to content

Commit

Permalink
feat: 新增柯里化函数题目
Browse files Browse the repository at this point in the history
  • Loading branch information
xjq committed Nov 16, 2022
1 parent 0ea1b42 commit 0ceca1c
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 10 deletions.
7 changes: 5 additions & 2 deletions client/src/pages/question/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import EditorConfig from '~store/config/editor';
import storage from '~utils/storage';
import { CodeStorageKey } from '~constant/storage';
import PageSpinner from '~components/PageSpinner';
import { parseConsoleOutput } from '~utils/helper';

function Question() {
const [searchParams] = useSearchParams();
Expand Down Expand Up @@ -94,7 +95,7 @@ function Question() {
const code = getEditor()?.getValue() || '';
const res = await exec({ code, name: detail?.name || '' });
setShowOutput(true);
setOutput(res.output.split(/\r\n/));
setOutput(parseConsoleOutput(res.output));
} catch (error) {
} finally {
setSubmitLoading(false);
Expand Down Expand Up @@ -145,7 +146,9 @@ function Question() {
{showOutput && (
<div className={styles.output}>
{output.map((str, index) => (
<p key={index}>{str}</p>
<pre className="text-gray-800" key={index}>
{str}
</pre>
))}
</div>
)}
Expand Down
5 changes: 4 additions & 1 deletion client/src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ export enum OutputType {
terminal,
}

export function parseConsoleOutput(output: string, type: OutputType) {
export function parseConsoleOutput(
output: string,
type: OutputType = OutputType.plain
) {
if (!output) return [];
// 换行解析
let splitAsEnter = output.split(/%0A/).map((str) => {
Expand Down
12 changes: 12 additions & 0 deletions question/FrontEnd/curry/answer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function f(fn) {
let _args = [];
return (...args) => {
let result = null;
if (args.length) {
_args = _args.concat(args);
} else {
result = fn(..._args);
}
return result;
};
}
66 changes: 66 additions & 0 deletions question/FrontEnd/curry/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## Curry

实现一个柯里化函数

未传递参数时真正调用

用例 1:

```js
const add = (...args) => args.reduce((a, b) => a + b, 0);

// 输入 [add, [1], [2], [3], null]

// 执行
const curryAdd = currying(add); // true
curryAdd(1); // null
curryAdd(2); // null
curryAdd(3); // null
curryAdd(); // 6

// [true, null, null, null, 6]
```

用例 2:

```js
const add = (...args) => args.reduce((a, b) => a + b, 0);

// 输入 [add, [1, 2, 1], null, null, null]
const curryAdd = currying(add); // true
curryAdd(1, 2, 1); // null
curryAdd(); // 4
curryAdd(); // 4
curryAdd(); // 4

// [true, null, 4, 4, 4]
```

用例 3:

```js
const add = (...args) => args.reduce((a, b) => a + b, 0);

// 输入 [add, [1, 2, 1], null, [1, 2], null]
const curryAdd = currying(add); // true
curryAdd(1, 2, 1); // null
curryAdd(); // 4
curryAdd(1, 2); // null
curryAdd(); // 7

// [true, null, 4, null, 7]
```

用例 4:

```js
const multi = (...args) => args.reduce((a, b) => a * b, 1);

// 输入 [multi, [100], null, [1, 2], null]
const curryMulti = currying(multi);
curryMulti(100); // null
curryMulti(); // 100
curryMulti(1, 2); // null
curryMulti(); // 200
// [true, null, 100, null, 200]
```
10 changes: 10 additions & 0 deletions question/FrontEnd/curry/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* 柯里化函数
*
* @export
* @param {Function} fn
* @return {Function}
*/
export default function currying(fn) {
return fn;
}
63 changes: 63 additions & 0 deletions question/FrontEnd/curry/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import assert from 'assert';
import f from './index.mjs';

const add = (...args) => args.reduce((a, b) => a + b, 0);
const multi = (...args) => args.reduce((a, b) => a * b, 1);

const cases = [
{
Input: [add, [1], [2], [3], null],
Expected: [true, null, null, null, 6],
Message: '加法函数柯里化一',
},
{
Input: [add, [1, 2, 1], null, null, null],
Expected: [true, null, 4, 4, 4],
Message: '加法函数柯里化二',
},
{
Input: [add, [1, 2, 1], null, [1, 2], null],
Expected: [true, null, 4, null, 7],
Message: '加法函数柯里化三',
},
{
Input: [multi, [100], null, [1, 2], null],
Expected: [true, null, 100, null, 200],
Message: '乘法函数柯里化一',
},
];

(function () {
for (let i = 0; i < cases.length; i++) {
const { Input, Expected, Message } = cases[i];
const output = [];
try {
let curry;
Input.forEach((arg) => {
let result;
if (!curry) {
curry = f(arg);
result = !!curry;
} else if (arg) {
result = curry(...arg);
} else {
result = curry();
}
output.push(result);
});

assert.deepEqual(output, Expected);
} catch (error) {
console.log('用例 ' + String(i + 1) + ': ' + Message + ' 未通过');
if (error.code === 'ERR_ASSERTION') {
console.log('Input:', 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 + ' 通过');
}
})();
4 changes: 2 additions & 2 deletions question/FrontEnd/deepClone/index.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* 深拷贝
*
* @param {*} obj
* @return {*}
* @param {(Array|Object)} obj
* @return {(Array|Object)}
*/
export default function deepClone(obj) {
return {};
Expand Down
6 changes: 3 additions & 3 deletions question/FrontEnd/flatten/index.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* 扁平化
* 数组扁平化
*
* @param {*} arr
* @return {*}
* @param {number[]} arr
* @return {number[]}
*/
export default function flatten(arr) {
return [];
Expand Down
9 changes: 8 additions & 1 deletion question/FrontEnd/sort/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export default function f(arr) {
/**
* 排序函数
*
* @export
* @param {number[]} arr
* @return {number[]}
*/
export default function sort(arr) {
return [];
}
7 changes: 7 additions & 0 deletions question/FrontEnd/unique/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* 数组去重
*
* @export
* @param {number[]} arr
* @return {number[]}
*/
export default function unique(arr) {
return [];
}
2 changes: 1 addition & 1 deletion server/src/qsdata/questions.json

Large diffs are not rendered by default.

0 comments on commit 0ceca1c

Please sign in to comment.