2023-04-23 22:41:08 +08:00
{
"data" : {
"question" : {
"questionId" : "2741" ,
"questionFrontendId" : "2629" ,
"categoryTitle" : "JavaScript" ,
"boundTopicId" : 2184269 ,
"title" : "Function Composition" ,
"titleSlug" : "function-composition" ,
2023-12-09 18:42:21 +08:00
"content" : "<p>Given an array of functions <code>[f<span style=\"font-size: 10.8333px;\">1</span>, f<sub>2</sub>, f<sub>3</sub>, ..., f<sub>n</sub>]</code>, return a new function <code>fn</code> that is the <strong>function composition</strong> of the array of functions.</p>\n\n<p>The <strong>function composition</strong> of <code>[f(x), g(x), h(x)]</code> is <code>fn(x) = f(g(h(x)))</code>.</p>\n\n<p>The <strong>function composition</strong> of an empty list of functions is the <strong>identity function</strong> <code>f(x) = x</code>.</p>\n\n<p>You may assume each function in the array accepts one integer as input and returns one integer as output.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [x => x + 1, x => x * x, x => 2 * x], x = 4\n<strong>Output:</strong> 65\n<strong>Explanation:</strong>\nEvaluating from right to left ...\nStarting with x = 4.\n2 * (4) = 8\n(8) * (8) = 64\n(64) + 1 = 65\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [x => 10 * x, x => 10 * x, x => 10 * x], x = 1\n<strong>Output:</strong> 1000\n<strong>Explanation:</strong>\nEvaluating from right to left ...\n10 * (1) = 10\n10 * (10) = 100\n10 * (100) = 1000\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> functions = [], x = 42\n<strong>Output:</strong> 42\n<strong>Explanation:</strong>\nThe composition of zero functions is the identity function</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code><font face=\"monospace\">-1000 <= x <= 1000</font></code></li>\n\t<li><code><font face=\"monospace\">0 <= functions.length <= 1000</font></code></li>\n\t<li>all functions accept and return a single integer</li>\n</ul>\n" ,
2023-04-23 22:41:08 +08:00
"translatedTitle" : "复合函数" ,
2023-12-09 18:42:21 +08:00
"translatedContent" : "<p>请你编写一个函数,它接收一个函数数组 <code>[f<sub>1</sub>, f<sub>2</sub>, f<sub>3</sub>,…, f<sub>n</sub>]</code> ,并返回一个新的函数 <code>fn</code> ,它是函数数组的 <strong>复合函数</strong> 。</p>\n\n<p><code>[f(x), g(x), h(x)]</code> 的 <strong>复合函数</strong> 为 <code>fn(x) = f(g(h(x)))</code> 。</p>\n\n<p>一个空函数列表的 <strong>复合函数</strong> 是 <strong>恒等函数</strong> <code>f(x) = x</code> 。</p>\n\n<p>你可以假设数组中的每个函数接受一个整型参数作为输入,并返回一个整型作为输出。</p>\n\n<p> </p>\n\n<p><strong class=\"example\">示例 1: </strong></p>\n\n<pre>\n<strong>输入:</strong>functions = [x => x + 1, x => x * x, x => 2 * x], x = 4\n<b>输出:</b>65\n<strong>解释:</strong>\n从右向左计算......\nStarting with x = 4.\n2 * (4) = 8\n(8) * (8) = 64\n(64) + 1 = 65\n</pre>\n\n<p><strong class=\"example\">示例 2: </strong></p>\n\n<pre>\n<b>输出:</b>functions = [x => 10 * x, x => 10 * x, x => 10 * x], x = 1\n<b>输入:</b>1000\n<strong>解释:</strong>\n从右向左计算......\n10 * (1) = 10\n10 * (10) = 100\n10 * (100) = 1000\n</pre>\n\n<p><strong class=\"example\">示例 3: </strong></p>\n\n<pre>\n<b>输入:</b>functions = [], x = 42\n<b>输出:</b>42\n<strong>解释:</strong>\n空函数列表的复合函数就是恒等函数</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code><font face=\"monospace\">-1000 <= x <= 1000</font></code></li>\n\t<li><code><font face=\"monospace\">0 <= functions.length <= 1000</font></code></li>\n\t<li><font face=\"monospace\">所有函数都接受并返回一个整型</font></li>\n</ul>\n" ,
2023-04-23 22:41:08 +08:00
"isPaidOnly" : false ,
"difficulty" : "Easy" ,
2023-12-09 18:42:21 +08:00
"likes" : 21 ,
2023-04-23 22:41:08 +08:00
"dislikes" : 0 ,
"isLiked" : null ,
"similarQuestions" : "[]" ,
"contributors" : [ ] ,
2023-12-09 18:42:21 +08:00
"langToValidPlayground" : "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}" ,
2023-04-23 22:41:08 +08:00
"topicTags" : [ ] ,
"companyTagStats" : null ,
"codeSnippets" : [
{
"lang" : "JavaScript" ,
"langSlug" : "javascript" ,
2023-12-09 18:42:21 +08:00
"code" : "/**\n * @param {Function[]} functions\n * @return {Function}\n */\nvar compose = function(functions) {\n \n\treturn function(x) {\n \n }\n};\n\n/**\n * const fn = compose([x => x + 1, x => 2 * x])\n * fn(4) // 9\n */" ,
2023-04-23 22:41:08 +08:00
"__typename" : "CodeSnippetNode"
} ,
{
"lang" : "TypeScript" ,
"langSlug" : "typescript" ,
2023-12-09 18:42:21 +08:00
"code" : "type F = (x: number) => number;\n\nfunction compose(functions: F[]): F {\n \n\treturn function(x) {\n \n }\n};\n\n/**\n * const fn = compose([x => x + 1, x => 2 * x])\n * fn(4) // 9\n */" ,
2023-04-23 22:41:08 +08:00
"__typename" : "CodeSnippetNode"
}
] ,
2023-12-09 19:57:46 +08:00
"stats" : "{\"totalAccepted\": \"8.5K\", \"totalSubmission\": \"10.4K\", \"totalAcceptedRaw\": 8526, \"totalSubmissionRaw\": 10400, \"acRate\": \"82.0%\"}" ,
2023-04-23 22:41:08 +08:00
"hints" : [
"Start by returning a function that takes in a number and returns a number." ,
"Call each of the functions in the correct order. Each time passing the output of the previous function into the next function."
] ,
"solution" : null ,
"status" : null ,
"sampleTestCase" : "[x => x + 1, x => x * x, x => 2 * x]\n4" ,
"metaData" : "{\n \"name\": \"compose\",\n \"params\": [\n {\n \"name\": \"functions\",\n \"type\": \"string\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"x\"\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 ,
2023-12-09 18:42:21 +08:00
"envInfo" : "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 5.1.6<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}" ,
2023-04-23 22:41:08 +08:00
"book" : null ,
"isSubscribed" : false ,
"isDailyQuestion" : false ,
"dailyRecordStatus" : null ,
"editorType" : "CKEDITOR" ,
"ugcQuestionId" : null ,
"style" : "LEETCODE" ,
"exampleTestcases" : "[x => x + 1, x => x * x, x => 2 * x]\n4\n[x => 10 * x, x => 10 * x, x => 10 * x]\n1\n[]\n42" ,
"__typename" : "QuestionNode"
}
}
}