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

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

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

问题

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

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

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

function getParentNodes(input) {
  const nodes = input.match(/<[^<>]+>/g); // 从输入中提取所有节点
  const parentNodes = [];
  let currentParentNode = '';

  for (const node of nodes) {
    if (currentParentNode === '') {
      currentParentNode = node;
    } else if (node.startsWith(currentParentNode)) {
      currentParentNode += ',' + node;
    } else {
      parentNodes.push(currentParentNode);
      currentParentNode = node;
    }
  }

  if (currentParentNode !== '') {
    parentNodes.push(currentParentNode);
  }

  return parentNodes;
}

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

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

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

function getParentNodes(input) {
  const nodes = input.match(/&lt;[^&lt;&gt;]+&gt;/g); // Extract all nodes from the input
  const parentNodes = [];
  let currentParentNode = &#39;&#39;;

  for (const node of nodes) {
    if (currentParentNode === &#39;&#39;) {
      currentParentNode = node;
    } else if (node.startsWith(currentParentNode)) {
      currentParentNode += &#39;,&#39; + node;
    } else {
      parentNodes.push(currentParentNode);
      currentParentNode = node;
    }
  }
  
  if (currentParentNode !== &#39;&#39;) {
    parentNodes.push(currentParentNode);
  }

  return parentNodes;
}

答案1

得分: 1

以下是您要翻译的内容:

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

const nodes_string = '<apple, <pear>>, <orange, <apple>, <cherry>, banana>, <banana, <pear>, <orange>>';
const nodes_string2 = 'doughnut, <apple, <pear>>, muffin, crumpet, <orange, <apple>, <cherry>, banana>, <banana, <pear>, <orange>>, pie';

const clean_text_nodes = (node) =>
  node.startsWith('<') ?
    node :
    node
      .split(',')
      .map((str) => str.trim())
      .filter((str) => str !== '');

const get_top_nodes = (nodes_string) => {
  let depth = 0;
  const result = [''];

  for (const char of nodes_string) {
    if (char === '<') {
      if (depth === 0) result.push('');
      depth++;
    }

    result[result.length - 1] += char;

    if (char === '>') {
      depth--;
      if (depth === 0) result.push('');
    }
  }

  return result.flatMap(clean_text_nodes);
};

console.log(get_top_nodes(nodes_string));
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 -->

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;;
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;;

const clean_text_nodes = (node) =&gt;
  node.startsWith(&#39;&lt;&#39;) ?
    node :
    node
      .split(&#39;,&#39;)
      .map( (str) =&gt; str.trim() )
      .filter( (str) =&gt; str !== &#39;&#39; );

const get_top_nodes = (nodes_string) =&gt; {
  let depth = 0;
  const result = [&#39;&#39;];
  
  for(const char of nodes_string) {
    if(char === &#39;&lt;&#39;) {
      if(depth === 0) result.push(&#39;&#39;);
      depth++;
    }
    
    result[result.length - 1] += char;
    
    if(char === &#39;&gt;&#39;) {
      depth--;
      if(depth === 0) result.push(&#39;&#39;);
    }
  }
  
  return result.flatMap(clean_text_nodes);
};

console.log(get_top_nodes(nodes_string));
console.log(get_top_nodes(nodes_string2));

<!-- end snippet -->

答案2

得分: 1

只返回翻译好的部分:

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

const parse = str => {
  const result = [];
  let sub = '',
    opened = 0,
    closed = 0;
  for (const s of str) {
    if (s === '<') ++opened;
    else if (s === '>') ++closed;
    if (opened > 0) {
      sub += s;
      if (opened === closed) {
        opened = closed = 0;
        result.push(sub);
        sub = '';
      }
    }
  }
  return result;
}

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 -->

const parse = str =&gt; {
  const result = [];
  let sub = &#39;&#39;,
    opened = 0,
    closed = 0;
  for (const s of str) {
    if (s === &#39;&lt;&#39;) ++opened;
    else if (s === &#39;&gt;&#39;) ++closed;
    if (opened &gt; 0) {
      sub += s;
      if (opened === closed) {
        opened = closed = 0;
        result.push(sub);
        sub = &#39;&#39;;
      }
    }
  }
  return result;
}

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:

确定