获取外部尖括号节点(算法)

huangapple go评论94阅读模式
英文:

How can I get the outer only angle brackets nodes (algorithm)

问题

I understand your request. Here's the translated code and explanation:

  1. const regexBycomma = /([^,<]*(?:<[^>]*>[^,<]*)*),?/g;
  2. str.split(regexBycomma);

Explanation: 正则表达式 regexBycomma 用于匹配逗号分隔的字符串,但要排除尖括号内的内容。然后使用这个正则表达式将输入字符串 str 拆分成数组。

  1. function getParentNodes(input) {
  2. const nodes = input.match(/<[^<>]+>/g); // 从输入中提取所有节点
  3. const parentNodes = [];
  4. let currentParentNode = '';
  5. for (const node of nodes) {
  6. if (currentParentNode === '') {
  7. currentParentNode = node;
  8. } else if (node.startsWith(currentParentNode)) {
  9. currentParentNode += ',' + node;
  10. } else {
  11. parentNodes.push(currentParentNode);
  12. currentParentNode = node;
  13. }
  14. }
  15. if (currentParentNode !== '') {
  16. parentNodes.push(currentParentNode);
  17. }
  18. return parentNodes;
  19. }

Explanation: 这是一个函数,用于找出输入字符串中的父节点。它首先使用正则表达式提取所有节点,并然后循环遍历这些节点,根据节点的嵌套关系将它们组合成父节点。最后,返回包含父节点的数组。

I've provided the translated code without any additional content, as requested.

英文:

Having this as input &#39;&lt;apple, &lt;pear&gt;&gt;, &lt;orange, &lt;apple&gt;, &lt;cherry&gt;, banana&gt;, &lt;banana, &lt;pear&gt;, &lt;orange&gt;&gt;&#39; having in mind that whatever is inside angle brackets is considered a node, so the output should be:
[&#39;&lt;apple, &lt;pear&gt;&gt;&#39;, &#39;&lt;orange, &lt;apple&gt;, &lt;cherry&gt;, banana&gt;&#39;, &#39;&lt;banana, &lt;pear&gt;, &lt;orange&gt;&gt;&#39;]

I've tried regex and split like this

  1. const regexBycomma = /([^,&lt;]*(?:&lt;[^&gt;]*&gt;[^,&lt;]*)*),?/g;
  2. str.split(regexBycomma);

with not good results, also many algorithms like this one:

  1. function getParentNodes(input) {
  2. const nodes = input.match(/&lt;[^&lt;&gt;]+&gt;/g); // Extract all nodes from the input
  3. const parentNodes = [];
  4. let currentParentNode = &#39;&#39;;
  5. for (const node of nodes) {
  6. if (currentParentNode === &#39;&#39;) {
  7. currentParentNode = node;
  8. } else if (node.startsWith(currentParentNode)) {
  9. currentParentNode += &#39;,&#39; + node;
  10. } else {
  11. parentNodes.push(currentParentNode);
  12. currentParentNode = node;
  13. }
  14. }
  15. if (currentParentNode !== &#39;&#39;) {
  16. parentNodes.push(currentParentNode);
  17. }
  18. return parentNodes;
  19. }

答案1

得分: 1

以下是您要翻译的内容:

这里的想法是通过在遇到&lt;时增加深度,遇到&gt;时减少深度来跟踪节点的深度。当深度变为零时,开始一个新的字符串。

  1. const nodes_string = '<apple, <pear>>, <orange, <apple>, <cherry>, banana>, <banana, <pear>, <orange>>';
  2. const nodes_string2 = 'doughnut, <apple, <pear>>, muffin, crumpet, <orange, <apple>, <cherry>, banana>, <banana, <pear>, <orange>>, pie';
  3. const clean_text_nodes = (node) =>
  4. node.startsWith('<') ?
  5. node :
  6. node
  7. .split(',')
  8. .map((str) => str.trim())
  9. .filter((str) => str !== '');
  10. const get_top_nodes = (nodes_string) => {
  11. let depth = 0;
  12. const result = [''];
  13. for (const char of nodes_string) {
  14. if (char === '<') {
  15. if (depth === 0) result.push('');
  16. depth++;
  17. }
  18. result[result.length - 1] += char;
  19. if (char === '>') {
  20. depth--;
  21. if (depth === 0) result.push('');
  22. }
  23. }
  24. return result.flatMap(clean_text_nodes);
  25. };
  26. console.log(get_top_nodes(nodes_string));
  27. console.log(get_top_nodes(nodes_string2));
英文:

Here the idea is to track how deep you are in to the nodes by adding to the depth when encountering a &lt; and reducing it when encounting a &gt;. When the depth becomes zero start a new string.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const nodes_string = &#39;&lt;apple, &lt;pear&gt;&gt;, &lt;orange, &lt;apple&gt;, &lt;cherry&gt;, banana&gt;, &lt;banana, &lt;pear&gt;, &lt;orange&gt;&gt;&#39;;
  2. const nodes_string2 = &#39;doughnut, &lt;apple, &lt;pear&gt;&gt;, muffin, crumpet, &lt;orange, &lt;apple&gt;, &lt;cherry&gt;, banana&gt;, &lt;banana, &lt;pear&gt;, &lt;orange&gt;&gt;, pie&#39;;
  3. const clean_text_nodes = (node) =&gt;
  4. node.startsWith(&#39;&lt;&#39;) ?
  5. node :
  6. node
  7. .split(&#39;,&#39;)
  8. .map( (str) =&gt; str.trim() )
  9. .filter( (str) =&gt; str !== &#39;&#39; );
  10. const get_top_nodes = (nodes_string) =&gt; {
  11. let depth = 0;
  12. const result = [&#39;&#39;];
  13. for(const char of nodes_string) {
  14. if(char === &#39;&lt;&#39;) {
  15. if(depth === 0) result.push(&#39;&#39;);
  16. depth++;
  17. }
  18. result[result.length - 1] += char;
  19. if(char === &#39;&gt;&#39;) {
  20. depth--;
  21. if(depth === 0) result.push(&#39;&#39;);
  22. }
  23. }
  24. return result.flatMap(clean_text_nodes);
  25. };
  26. console.log(get_top_nodes(nodes_string));
  27. console.log(get_top_nodes(nodes_string2));

<!-- end snippet -->

答案2

得分: 1

只返回翻译好的部分:

您可以天真地计算打开和关闭括号并将 "节点" 推送到结果:

  1. const parse = str => {
  2. const result = [];
  3. let sub = '',
  4. opened = 0,
  5. closed = 0;
  6. for (const s of str) {
  7. if (s === '<') ++opened;
  8. else if (s === '>') ++closed;
  9. if (opened > 0) {
  10. sub += s;
  11. if (opened === closed) {
  12. opened = closed = 0;
  13. result.push(sub);
  14. sub = '';
  15. }
  16. }
  17. }
  18. return result;
  19. }
  20. console.log(parse('<apple, <pear>>, <orange, <apple>>, <cherry>, banana>, <banana, <pear>>, <orange>>'))

这是一段 JavaScript 代码,用于解析包含嵌套尖括号的字符串。

英文:

You might just naively count opened and closed brackets and push "nodes" to the result:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const parse = str =&gt; {
  2. const result = [];
  3. let sub = &#39;&#39;,
  4. opened = 0,
  5. closed = 0;
  6. for (const s of str) {
  7. if (s === &#39;&lt;&#39;) ++opened;
  8. else if (s === &#39;&gt;&#39;) ++closed;
  9. if (opened &gt; 0) {
  10. sub += s;
  11. if (opened === closed) {
  12. opened = closed = 0;
  13. result.push(sub);
  14. sub = &#39;&#39;;
  15. }
  16. }
  17. }
  18. return result;
  19. }
  20. console.log(parse(&#39;&lt;apple, &lt;pear&gt;&gt;, &lt;orange, &lt;apple&gt;, &lt;cherry&gt;, banana&gt;, &lt;banana, &lt;pear&gt;, &lt;orange&gt;&gt;&#39;))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月31日 04:18:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定