循环遍历两个数组删除perl中的重叠
我有两组范围,由 [ start, stop ] 值表示。某些范围重叠,这意味着一个范围的起点位于另一范围的 [ start, stop ] 之间。我想创建一组没有此类重叠的新范围,并且范围中也不包含任何新值。
范围如下所示:
@starts @ends
5 108
5 187
44 187
44 229
44 236
64 236
104 236
580 644
632 770
我期望的输出将是这样的:
@starts @ends
5 236
580 770
这是因为前七个范围与 5 => 的间隔重叠。 236,最后两个与632 => 236的间隔重叠。 770.
这是我尝试过的代码:
$fix = 0;
foreach (@ends) {
if ($starts[$fix + 1] < $ends[$fix]) {
splice(@ends, $fix, $fix);
splice(@starts, $fix + 1, $fix + 1);
} else {
$fix += 1;
}
}
我可以自己打印出值,我只需要合并算法方面的帮助。
I have two sets of ranges, represented by [ start, stop ] values. Some of the ranges overlap, meaning that the start of one range is in between the [ start, stop ] of the other range. I'd like to make a new set of ranges that has no such overlap, and also doesn't include any new values in a range.
The ranges look like this:
@starts @ends
5 108
5 187
44 187
44 229
44 236
64 236
104 236
580 644
632 770
The output that I expect would be this:
@starts @ends
5 236
580 770
This is because the first seven ranges overlap with the interval from 5 => 236, and the last two overlap with the interval from 632 => 770.
Here's the code that I tried:
$fix = 0;
foreach (@ends) {
if ($starts[$fix + 1] < $ends[$fix]) {
splice(@ends, $fix, $fix);
splice(@starts, $fix + 1, $fix + 1);
} else {
$fix += 1;
}
}
I can print out the values myself, I just need help with the algorithm for merging.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这会就地编辑数组,当它们重叠时简单地折叠边界。
This edits your arrays in-place, simply collapsing boundaries when they overlap.
我认为这就是你想要的。您有一系列 [start,stop] 形式的范围,并且您想要合并重叠的范围。下面的方法相当简单。
原始集和合并集。
合并(非重叠)范围。
对于每个候选范围从
原始设置,你做出选择:
希望这是有道理的。从你的问题来看,这并不是你想要的,所以如果这不正确,请告诉我。
I think that this is what you want. You have a series of ranges of the form [start,stop], and you'd like to merge the overlapping ranges. The approach below is fairly simple.
original set and the merged set.
of merged (non-overlapping) ranges.
For each candidate range left from
the original set, you make a choice:
Hopefully this makes sense. It's not too obvious from your question that this is what you wanted, so let me know if this isn't right.
由于数组是按开始排序的,因此最简单的方法是从末尾开始:
Since the arrays are ordered by start, then the easiest is to work from the end:
我不熟悉 PERL,但以下伪代码解决方案可能可以轻松修改:
I am not fluent in PERL, but the following pseudocode solution can probably be easily adapted:
这怎么样?
How's this?