通过两个数组删除循环在Perl重叠数组、两个、Perl

2023-09-11 23:04:13 作者:無心何來痛

我有两套范围,通过psented再$ P $ [启动,停止]值。一些范围的重叠,这意味着一个范围的开始是在之间的[开始,停止]的其他范围。我想使一个新的范围集合不具有这样的重叠,也没有包括在一个范围内的任何新的值。

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.

的范围是这样的:

@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

这是因为前七范围从5 => 236的间隔重叠,并且最后两个重叠与来自632的间隔=> 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.

这里的code,我试过:

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.

推荐答案

该编辑您的阵列原地,只是倒塌的边界时,它们重叠。

This edits your arrays in-place, simply collapsing boundaries when they overlap.

# Since they're sorted by @starts, accept the 0th interval, start at 1
for (1..$#starts) {
    # extra check on array bounds, since we edit in-place
    last unless $_ < @starts;
    # don't need to collapse if no overlap with previous end
    next unless $starts[$_] <= $ends[$_-1];
    # delete this start and the previous end
    splice(@starts,$_,1);
    splice(@ends,$_-1,1);
    # rerun this loop for the same value of $_ since it was deleted
    redo;
}