mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
59 lines
7.2 KiB
JSON
59 lines
7.2 KiB
JSON
{
|
|
"data": {
|
|
"question": {
|
|
"questionId": "2740",
|
|
"questionFrontendId": "2632",
|
|
"boundTopicId": null,
|
|
"title": "Curry",
|
|
"titleSlug": "curry",
|
|
"content": "<p>Given a function <code>fn</code>, return a <strong>curried</strong> version of that function.</p>\n\n<p>A <strong>curried</strong> function is a function that accepts fewer or an equal number of parameters as the original function and returns either another <strong>curried</strong> function or the same value the original function would have returned.</p>\n\n<p>In practical terms, if you called the original function like <code>sum(1,2,3)</code>, you would call the <strong>curried</strong> version like <code>csum(1)(2)(3)<font face=\"sans-serif, Arial, Verdana, Trebuchet MS\">, </font></code><code>csum(1)(2,3)</code>, <code>csum(1,2)(3)</code>, or <code>csum(1,2,3)</code>. All these methods of calling the <strong>curried</strong> function should return the same value as the original.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[1],[2],[3]]\n<strong>Output:</strong> 6\n<strong>Explanation:</strong>\nThe code being executed is:\nconst curriedSum = curry(fn);\ncurriedSum(1)(2)(3) === 6;\ncurriedSum(1)(2)(3) should return the same value as sum(1, 2, 3).\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong>\nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[1,2],[3]]]\n<strong>Output:</strong> 6\n<strong>Explanation:</strong>\ncurriedSum(1, 2)(3) should return the same value as sum(1, 2, 3).</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong>\nfn = function sum(a, b, c) { return a + b + c; }\ninputs = [[],[],[1,2,3]]\n<strong>Output:</strong> 6\n<strong>Explanation:</strong>\nYou should be able to pass the parameters in any way, including all at once or none at all.\ncurriedSum()()(1, 2, 3) should return the same value as sum(1, 2, 3).\n</pre>\n\n<p><strong class=\"example\">Example 4:</strong></p>\n\n<pre>\n<strong>Input:</strong>\nfn = function life() { return 42; }\ninputs = [[]]\n<strong>Output:</strong> 42\n<strong>Explanation:</strong>\ncurrying a function that accepts zero parameters should effectively do nothing.\ncurriedLife() === 42\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 <= inputs.length <= 1000</code></li>\n\t<li><code>0 <= inputs[i][j] <= 10<sup>5</sup></code></li>\n\t<li><code>0 <= fn.length <= 1000</code></li>\n\t<li><code>inputs.flat().length == fn.length</code></li>\n\t<li><code>function parameters explicitly defined</code></li>\n</ul>\n",
|
|
"translatedTitle": null,
|
|
"translatedContent": null,
|
|
"isPaidOnly": false,
|
|
"difficulty": "Medium",
|
|
"likes": 15,
|
|
"dislikes": 2,
|
|
"isLiked": null,
|
|
"similarQuestions": "[{\"title\": \"Memoize\", \"titleSlug\": \"memoize\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Memoize II\", \"titleSlug\": \"memoize-ii\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]",
|
|
"exampleTestcases": "function sum(a, b, c) { return a + b + c; }\n[[1],[2],[3]]\nfunction sum(a, b, c) { return a + b + c; }\n[[1,2],[3]]\nfunction sum(a, b, c) { return a + b + c; }\n[[],[],[1,2,3]]\nfunction life() { return 42; }\n[[]]",
|
|
"categoryTitle": "JavaScript",
|
|
"contributors": [],
|
|
"topicTags": [],
|
|
"companyTagStats": null,
|
|
"codeSnippets": [
|
|
{
|
|
"lang": "JavaScript",
|
|
"langSlug": "javascript",
|
|
"code": "/**\n * @param {Function} fn\n * @return {Function}\n */\nvar curry = function(fn) {\n return function curried() {\n\n };\n};\n\n/**\n * function sum(a, b) { return a + b; }\n * const csum = curry(sum);\n * csum(1)(2) // 3\n */\n",
|
|
"__typename": "CodeSnippetNode"
|
|
},
|
|
{
|
|
"lang": "TypeScript",
|
|
"langSlug": "typescript",
|
|
"code": "function curry(fn: Function): Function {\n return function curried() {\n\n };\n};\n\n/**\n * function sum(a, b) { return a + b; }\n * const csum = curry(sum);\n * csum(1)(2) // 3\n */\n",
|
|
"__typename": "CodeSnippetNode"
|
|
}
|
|
],
|
|
"stats": "{\"totalAccepted\": \"286\", \"totalSubmission\": \"326\", \"totalAcceptedRaw\": 286, \"totalSubmissionRaw\": 326, \"acRate\": \"87.7%\"}",
|
|
"hints": [
|
|
"You can access the count of parameters expected to passed into a function with \"fn.length\".",
|
|
"You can use recursion. If the length of params passed is equal to fn.length, you are done. Just pass those params to fn. Otherwise return a function that is includes the previous passed params plus the new params. The new function should contain a recursive call to curry()."
|
|
],
|
|
"solution": null,
|
|
"status": null,
|
|
"sampleTestCase": "function sum(a, b, c) { return a + b + c; }\n[[1],[2],[3]]",
|
|
"metaData": "{\n \"name\": \"curry\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"integer[][]\",\n \"name\": \"inputs\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
|
"judgerAvailable": true,
|
|
"judgeType": "large",
|
|
"mysqlSchemas": [],
|
|
"enableRunCode": true,
|
|
"enableTestMode": false,
|
|
"enableDebugger": false,
|
|
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
|
"libraryUrl": null,
|
|
"adminUrl": null,
|
|
"challengeQuestion": null,
|
|
"__typename": "QuestionNode"
|
|
}
|
|
}
|
|
} |