recursion 相关问题

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

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

我需要帮助从元组列表生成组合,其中每个元组包含索引 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

带递归的 XSLT 转换

我需要将 XML 文档转换为 HTML。 输入 XML: 大家好! 我需要将 XML 文档转换为 HTML。 输入XML: <items> <item no="1" par_no="0" user="Mike">Hi all!</item> <item no="2" par_no="1" user="Jane">Hi...</item> <item no="3" par_no="2" user="Pit"> says: wasap man?</item> <item no="4" par_no="2" user="Karol">are you ok?</item> <item no="5" par_no="4" user="Nick">I think he's not in the mood.</item> <item no="6" par_no="5" user="Karol">I just ask a question.</item> <item no="7" par_no="1" user="Pit">says: how are you?</item> <item no="8" par_no="7" user="Mike">fine!</item> </items> 输出 HTML: <?xml version="1.0" encoding="UTF-8"?> <ul> <li><b>Mike</b> says: Hi all! <ul> <li><b>Jane</b> says: Hi... <li><b>Pit</b> says: wasap man? <li><b>Karol</b> says: are you ok? <ul> <li><b>Nick</b> says: I tihnk he's not in the mood. <ul> <li><b>Karol</b> says: I just ask a question. </ul> </li> </ul> </li> </ul> </li> <li><b>Pit</b> says: how are you? <ul> <li><b>Mike</b> says: fine! </ul> </li> </ul> </li> </ul> 我试图解决这个问题。我得到了代码。正在对行进行排序,但我无法获取附件。我的 XSL: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" /> <xsl:template match="items"> <html> <ul> <xsl:apply-templates select="item[@par_no=0]"/> </ul> </html> </xsl:template> <xsl:template match="item"> <xsl:variable name="varNo" select="@no"/> <li> <b><xsl:value-of select="@user"/></b> <xsl:text> says: </xsl:text> <xsl:value-of select="."/> </li> <xsl:apply-templates select="//items/item[@par_no=$varNo]"> <xsl:sort select="@par_no" data-type="number"/> </xsl:apply-templates> </xsl:template> </xsl:stylesheet> 我不知道在哪里插入标签才能得到这样的嵌套结构。请帮忙! 在 XSLT 3 中,您可以按如下方式解决: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all"> <xsl:output method="html" version="5.0" indent="yes"/> <xsl:template match="/"> <html> <head> <title>Test</title> </head> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:key name="child" match="item" use="@par_no"/> <xsl:template match="items"> <ul> <xsl:apply-templates select="key('child', '0')"/> </ul> </xsl:template> <xsl:template match="item"> <li> <b><xsl:value-of select="@user"/></b> <xsl:text> says: </xsl:text> <xsl:value-of select="."/> <xsl:where-populated> <ul> <xsl:apply-templates select="key('child', @no)"/> </ul> </xsl:where-populated> </li> </xsl:template> </xsl:stylesheet> 密钥和模板在以前的版本中的工作方式相同,您需要将 xsl:where-populated 替换为检查 key('child', @no) 是否选择元素以输出 ul: <xsl:template match="item"> <li> <b><xsl:value-of select="@user"/></b> <xsl:text> says: </xsl:text> <xsl:value-of select="."/> <xsl:variable name="children" select="key('child', @no)"/> <xsl:if test="$children"> <ul> <xsl:apply-templates select="$children"/> </ul> </xsl:if> </li> </xsl:template> 我认为你可以简单地做(仅使用 XSLT 1.0): <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:key name="chld" match="item" use="@par_no" /> <xsl:template match="/items"> <ul> <xsl:apply-templates select="item[@par_no='0']"/> </ul> </xsl:template> <xsl:template match="item"> <li> <b> <xsl:value-of select="@user"/> </b> <xsl:text> says: </xsl:text> <xsl:value-of select="."/> <ul> <xsl:apply-templates select="key('chld', @no)"/> </ul> </li> </xsl:template> </xsl:stylesheet> 浏览器中显示的结果是: 是的,我致力于 XSLT 1.0。但我没有得到这个结果。在我的 Chrome 浏览器中,结果有所不同。不幸的是我无法发布图片(( 我需要像你的照片一样的结果! HTML: <ul> <li><b>Mike</b> says: Hi all! <ul> <li> <b>Jane</b> says: Hi... <ul> <li><b>Pit</b> says: asap man?</li> <li><b>Karol</b> says: are you ok? <ul> <li><b>Nick</b> says: I think he's not in the mood. <ul> <li><b>Karol</b> says: I just ask a question.</li> </ul> </li> </ul> </li> </ul> </li> <li><b>Pit</b> says: how are you? <ul> <li><b>Mike</b> says: fine!</li> </ul> </li> </ul> </li> </ul> 为什么说 Pit 会重复两次?

回答 3 投票 0

如何在循环中将找到的值求和到顶层?

我的类别有/没有子类别。 还有属于一个或多个类别的产品。 我需要计算所有父类别中的产品数量,包括子类别

回答 1 投票 0

Haskell 无限递归

以下函数计算斐波那契数列: fib = 0 : 1 : (zipWith (+) fib (尾部 fib)) 如果我们运行它,我们将得到一个无限列表,但是递归是如何工作的呢?为什么会变成...

回答 2 投票 0

调用列表中每个元素的函数

仅使用递归(即没有任何类型的循环),给定一个元素列表,如何在 OCaml 中每次使用该元素作为参数来每次为列表中的每个元素调用一个函数?佛...

回答 2 投票 0

递归函数寻找素因数

我做了一个递归函数来查找数字的质因数,但它有一个错误,导致 Turbo c 退出。请帮忙 #包括 #包括 int素数(int num); 整数

回答 12 投票 0

递归选择排序堆栈溢出错误

导入java.util.Arrays; 公共类 RecSelect { 公共静态无效选择(int [] ar,int idx,int min,int start){ if(idx == ar.length) { 返回; } 如果(id...

回答 1 投票 0

实现递归作为解决问题的工具有哪些权衡?

我用 C++ 实现了一个简单的递归平均计算器,其中“学生”必须输入他们的成绩,函数返回他们的平均值。但我想知道使用是否有任何优势

回答 1 投票 0

增加三元组子序列

我正在 leetcode.com 上尝试增加三元组子序列问题 我一开始采用了蛮力方法,但遇到了超时问题,但通过了几乎所有测试用例。以下...

回答 2 投票 0

如何预测堆栈溢出?堆栈中如何以及哪些内存存储?

我实现了一个洪水填充递归函数来检查玩家是否能够到达地图出口,或者它会被墙壁包围。所以一个简单的地图看起来像这样: 111111111 100010C01

回答 1 投票 0

配置深度溢出值-Start-Job

我有一个递归函数,执行了大约 750 次 - 迭代 XML 文件并进行处理。代码正在使用 Start-Job 运行 下面的例子: $job = 开始作业 -ScriptBlock { 有趣...

回答 2 投票 0

返回目录和子目录中的文件总数

尝试创建一个函数,返回找到目录及其子目录的文件数。只需要帮助即可开始

回答 7 投票 0

在 TypeScript 界面中推断 React 子 props

是否可以推断第一个子元素的 props 并在 TypeScript 中强制执行它们?我可以接近它,但是泛型开始失败,而且我无法推断出类型。 我是...

回答 1 投票 0

将偶数元素移动到数组的前面,同时保持相对顺序

public static void teePaarisPaaritud(int[] a){ teePaarisPaaritudRek(a, 0); } 私有静态 void teePaarisPaaritudRek(int[] a, int i) { if(i == a.length-1) 返回; if(a[i] % 2 != 0){ ...

回答 3 投票 0

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