英文:
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 '<apple, <pear>>, <orange, <apple>, <cherry>, banana>, <banana, <pear>, <orange>>'
having in mind that whatever is inside angle brackets is considered a node, so the output should be:
['<apple, <pear>>', '<orange, <apple>, <cherry>, banana>', '<banana, <pear>, <orange>>']
I've tried regex and split like this
const regexBycomma = /([^,<]*(?:<[^>]*>[^,<]*)*),?/g;
str.split(regexBycomma);
with not good results, also many algorithms like this one:
function getParentNodes(input) {
const nodes = input.match(/<[^<>]+>/g); // Extract all nodes from the input
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;
}
答案1
得分: 1
以下是您要翻译的内容:
这里的想法是通过在遇到<
时增加深度,遇到>
时减少深度来跟踪节点的深度。当深度变为零时,开始一个新的字符串。
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 <
and reducing it when encounting a >
. When the depth becomes zero start a new string.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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));
<!-- 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 => {
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>>'))
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论