recursion 相关问题

递归是一种函数调用,函数调用自身。这些函数也称为递归函数。结构递归是一种解决问题的方法,其中问题的解决方案取决于对同一问题的较小实例的解决方案。

递归查找数组中最大元素的索引

所以我正在研究这个问题,要求我使用递归来创建一个方法,使用此标头“private static int findLargest (...

回答 1 投票 0

追加到递归函数内的列表会更改所有项目吗?河内塔

我正在尝试使用 numpy 制作一个矩阵列表来表示解决河内塔问题的最小移动次数。当我在每次移动后打印 m 时,我的解决方案有效,但是当我尝试......

回答 2 投票 0

如何让一个函数记住2个变量函数值?

我想提高递归计算值的性能 n = 100 定义 T(n,k): q = 0 如果 n == 0 且 k == 0: 返回(1) q = 1 如果 k>n 或 n<0: retu...

回答 3 投票 0

Laravel 递归映射

我需要在 Laravel 中实现一个递归映射函数。我的数据看起来像这样。 如果 all_children 为空,那么我将停止调用递归... 我的代码看起来像这样,但它不起作用......

回答 2 投票 0

将递归函数转换为带有堆栈和 while 循环的迭代函数时如何传递值

给定一个函数 f,它从高度为 max_height 的二叉树的节点 (l, r) 计算某些内容,我想计算当我们将叶子加在一起并将总和与

回答 1 投票 0

有限集上的递归函数 - 如何证明终止

我(仍然)在有限集上的递归函数上苦苦挣扎。我想在有限(有限)集合上定义一个特殊的笛卡尔积。我正在尝试实现的简化示例...

回答 1 投票 0

在 Node.js 中递归查找文件路径

const fs = require('fs').promises; 异步函数 readMe(文件夹) { 让结果=[]; 尝试 { const content = wait fs.readdir(folder, { withFileTypes: true }); 对于(让

回答 1 投票 0

将以下递归二叉树遍历算法转换为迭代算法

我想了解以下递归代码是否可以通过迭代实现来提高效率,如果可以,如何实现这样的实现。我已经好几年了...

回答 1 投票 0

为什么'p = p.rest'不影响CS61A Hw06中Q5的结果变量

在迭代解决方案中,有谁知道为什么 p = p.rest 不影响结果变量?由于上面的代码 p.rest = Link(item) 只是修改了结果,那么添加 p = ... 的目的是什么

回答 2 投票 0

如何找出递归平方根猜测程序的基本情况

我正在解决的问题是: 古希腊人已知以下计算方法 平方根。给定值 x > 0 和平方根的猜测 g,a 更好的猜测是...

回答 2 投票 0

使用视图更新表并获取“在关系“员工”的规则中检测到无限递归

下面是我在 PostgreSQL 16 数据库中使用的代码。每次我尝试更新我得到的薪水时: 错误:在关系“员工”的规则中检测到无限递归 SQL 状态:42...

回答 1 投票 0

POSTGRESQL V16。使用视图更新表并获取“在关系“员工”的规则中检测到无限递归

下面是我正在使用的代码,每次我尝试更新我得到的工资时 “错误:在关系“员工”的规则中检测到无限递归 SQL 状态:42P17" 使用对象 创造...

回答 1 投票 0

按键递归地对嵌套的 OrderedDict 进行排序

假设 orig 是一个 OrderedDict,其中包含普通的 string:string 键值对,但有时该值可能是另一个嵌套的 OrderedDict。 我想按键按字母顺序(升序)对 orig 进行排序...

回答 6 投票 0

生成元组列表对的笛卡尔积

我需要帮助从元组列表生成组合,其中每个元组包含索引 0 处的字符串和索引 1 处的值列表。例如,考虑以下设置: 参数 = ['sn',...

回答 1 投票 0

计算整数中数字“7”的单返回递归解决方案

我正在 Codingbat 的 Recursion-1 部分寻求编程挑战方面的帮助,特别是 count7 问题。任务是计算给定的非

回答 16 投票 0

Mysql 8 具有两个强制父级的递归查询

我正在处理一个层次结构问题,其中节点有两个父节点,并且只需要获取两个父节点都在结果中的节点。 在快乐的世界里,这个查询将是完美的 与重复...

回答 1 投票 0

XML 脚本运行以打印所需的输出

我有一个示例 XML 输入文件,其中包含以下内容: 我有一个示例 XML 输入文件,其中包含以下内容: <?xml version="1.0" encoding="utf-8"?> <infobases> <infobase author="Chartered Professional Accountants of Canada" levelDefOrder="Level 1,Level 2,Level 3,Level 4,Level 5,Level 6,Level 7,Level 8,Level 9,Level 10,Level 11,Level 12,Level 13,Normal Level" levels="Level 1,Level 2,Level 3,Level 4,Level 5,Level 6,Level 7,Level 8,Level 9,Level 10,Level 11,Level 12,Level 13" name="info_a" title="CPA Canada Standards and Guidance Collection"> <file level="Level 1" heading="XYZ1-L1">XYZ1-L1 <file level="Level 2" heading="XYZ1-L12">XYZ1-L12 <file level="Level 3" heading="XYZ1-L123">XYZ1-L123</file> <file level="Level 3" heading="XYZ1-L123">XYZ1-L123</file> <file level="Level 3" heading="XYZ1-L123">XYZ1-L123</file> </file> </file> <file level="Level 1" heading="XYZ2-L1">XYZ2-L1</file> <file level="Level 1" heading="XYZ2-L1">XYZ2-L1 <file level="Level 2" heading="XYZ2-L12">XYZ2-L12</file> <file level="Level 2" heading="XYZ2-L123">XYZ2-L123 <file level="Level 3" heading="XYZ1-L123">XYZ1-L123 <file level="Level 4" heading="XYZ1-L123">XYZ1-L123</file> </file> </file> </file> </infobase> </infobases> 我想编写一个脚本来识别文件元素级别属性,并在打印文件元素的标题时给出适当的缩进。文件元素标题将获得 .ditamap 扩展名,该扩展名将其他文件元素作为子元素并忽略其他子元素(不在示例 xml 文件中)。如果文件元素没有子文件元素,则会添加 .dita 扩展名。 我用 javascript 编写了一个脚本,它倾向于进行正确的缩进,但分配的扩展名不正确。我得到所有文件元素标题的 .dita。这是代码: const fs = require('fs'); const XmlStream = require('xml-stream'); // Create a readable stream from the XML file const stream = fs.createReadStream('input1.xml'); // Create a writable stream to the output text file const outputStream = fs.createWriteStream('output.txt'); // Create a new XML stream parser const xmlParser = new XmlStream(stream); // Function to print headings with proper indentation function printHeadingsToFile(file, indentation = '') { // Calculate the indentation based on the level of the file const levelIndentation = ' '.repeat(parseInt(file.$.level.substr(6)) - 1); // Determine file extension based on child elements const fileExtension = file.file ? 'ditamap' : 'dita'; // Write the heading with indentation to the output file outputStream.write(`${indentation}${levelIndentation}${file.$.heading}.${fileExtension}\n`); // Check if there are nested files if (file.file) { // If nested files exist, recursively print their headings with increased indentation file.file.forEach(nestedFile => { printHeadingsToFile(nestedFile, `${indentation}${levelIndentation}`); }); } } // Event listener for when a new XML element is encountered xmlParser.on('startElement: file', function(element) { // Print headings for each file printHeadingsToFile(element); }); // Event listener for when the parsing ends xmlParser.on('end', function() { console.log('Parsing finished.'); // Close the output stream after finishing writing outputStream.end(); }); // Event listener for any errors during parsing xmlParser.on('error', function(err) { console.error('Error during parsing:', err); // Close the output stream if there is an error outputStream.end(); }); 我在这里得到的输出如下: XYZ1-L1.dita XYZ1-L12.dita XYZ1-L123.dita XYZ1-L123.dita XYZ1-L123.dita XYZ2-L1.dita XYZ2-L1.dita XYZ2-L12.dita XYZ2-L123.dita XYZ1-L123.dita XYZ1-L123.dita 预期输出: XYZ1-L1.ditamap XYZ1-L12.ditamap XYZ1-L123.dita XYZ1-L123.dita XYZ1-L123.dita XYZ2-L1.dita XYZ2-L1.ditamap XYZ2-L12.dita XYZ2-L123.ditamap XYZ1-L123.ditamap XYZ1-L123.dita 找到了使用不同 xml 解析器包的解决方案。 const fs = require('fs'); const sax = require('sax'); // Create a SAX parser const parser = sax.parser(true); // Create a write stream to a text file const outputStream = fs.createWriteStream('output.txt'); // Array to store file attributes let fileAttributes = []; // Event handlers for the SAX parser parser.onopentag = function (node) { // Check if the current node is a <file> element if (node.name === 'file') { // Extract attributes and push them to the array fileAttributes.push(node.attributes); } }; parser.onend = function () { // Parsing ends, log the final output //console.log('File attributes:', fileAttributes); // Function to determine the file extension function getFileExtension(currentLevel, nextLevel) { if (currentLevel < nextLevel) { return '.ditamap'; } else { return '.dita'; } } // Iterate through the fileAttributes array for (let i = 0; i < fileAttributes.length; i++) { const currentFile = fileAttributes[i]; const nextFile = fileAttributes[i + 1]; // Get the level numbers const currentLevel = parseInt(currentFile.level.match(/\d+/)[0]); const nextLevel = nextFile ? parseInt(nextFile.level.match(/\d+/)[0]) : 0; // Get the file extension const extension = getFileExtension(currentLevel, nextLevel); // Indentation based on the level const indentation = ' '.repeat((currentLevel - 1) * 4); // Prepare the text to write const textToWrite = indentation + currentFile.heading + extension + '\n'; // Write the text to the output stream outputStream.write(textToWrite); } // Close the output stream when done outputStream.end(); }; // Read XML file const xmlData = fs.readFileSync('input.xml', 'utf8'); // Parse XML data parser.write(xmlData).close(); 结果输出: L1.ditamap L2.ditamap L3.dita L3.dita L3.dita L1.dita L1.ditamap L2.dita L2.ditamap L3.ditamap L4.ditamap Level5Text.dita

回答 1 投票 0

负指数的平方

我不确定平方幂是否可以处理负指数。我实现了以下代码,该代码仅适用于正数。 #包括 int power(int x, int exp...

回答 1 投票 0

如何递归获取指定路径上node_modules的文件夹总大小?

我想知道这个node_modules文件夹在我的机器上占用了多少空间。 我可以使用什么命令行来在指定路径内递归获取此 node_modules 的总大小?

回答 1 投票 0

输出返回2而不是0

这是我的切入片段的解决方案 给定一个整数“N”,表示杆的长度。您需要确定可以用该杆制作的最大分段数,前提是每个

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.