Re: Calculate the overlap length between two ranges
by poolpi (Hermit) on Oct 30, 2013 at 12:17 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
use Set::IntSpan;
my $set1 = new Set::IntSpan '31-52';
my $set2 = new Set::IntSpan '27-50';
my $u_set = intersect $set1 $set2;
my @set = sets $u_set;
print @set;
__END__
output:
31-50
Update: code + typo
hth, PooLpi
The stone that the builder refused, will always be the head cornerstone.
[ Robert Nesta MARLEY ]
| [reply] [d/l] |
Re: Calculate the overlap length between two ranges
by hdb (Monsignor) on Oct 30, 2013 at 12:27 UTC
|
use strict;
use warnings;
use List::Util qw(min max);
my $range1 = "31-52";
my $range2 = "27-50";
print max(min($2,$4)-max($1,$3)+1,0),"\n" if "$range1,$range2"=~/(\d+)
+-(\d+),(\d+)-(\d+)/;
Of course, each max or min could be replaced by the ternary operator ?:. | [reply] [d/l] [select] |
Re: Calculate the overlap length between two ranges
by toolic (Bishop) on Oct 30, 2013 at 12:17 UTC
|
| [reply] |
|
|
Thanks guys, will look for your suggestions!
Cheers!
| [reply] |
Re: Calculate the overlap length between two ranges
by Athanasius (Cardinal) on Oct 30, 2013 at 12:26 UTC
|
#! perl
use strict;
use warnings;
use Set::Scalar;
my $range1 = Set::Scalar->new(5 .. 25);
my $range2 = Set::Scalar->new(7 .. 31);
my $common = $range1->intersection($range2);
my @members = $common->members();
print join(', ', sort { $a <=> $b } @members), "\n";
Output:
22:25 >perl 764_SoPW.pl
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 2
+5
22:25 >
Hope that helps,
| [reply] [d/l] [select] |
Re: Calculate the overlap length between two ranges
by LanX (Saint) on Oct 30, 2013 at 13:13 UTC
|
What have you tried to solve your homework?
Your reaction to McA is unacceptable !
Maybe better try searching for older discussion in the archive you're by far not the first one with this question.
No more feeding...
Cheers Rolf
( addicted to the Perl Programming Language)
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: Calculate the overlap length between two ranges
by McA (Priest) on Oct 30, 2013 at 12:11 UTC
|
Yes, there is. You're welcome.
Regards
McA
| [reply] |
|
|
LOL
Given the many, many really good bits of advice I have seen you give, McA, I really have to ask -- are you feeling particularly feisty this morning? :-)
It's just that I don't recall you having a solid trend of picking on people for asking the wrong question. Or maybe I just never noticed it before?
| [reply] [d/l] [select] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: Calculate the overlap length between two ranges
by Laurent_R (Canon) on Oct 30, 2013 at 12:52 UTC
|
I can think of numerous ways of calculating these types of things, but I wonder how you define exactly the length between some overlapping numerical ranges. This is not clear to me. Do you mean the number of integers belonging to the overlap? Or something else?
| [reply] |
Re: Calculate the overlap length between two ranges
by Cristoforo (Curate) on Oct 30, 2013 at 19:45 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw/ min max /;
my $hash = overlap('31-52', '27-50');
use Data::Dumper; print Dumper $hash;
sub overlap {
my ($r1_lo, $r1_hi, $r2_lo, $r2_hi) = map {split /-/} @_;
my $min = max($r1_lo, $r2_lo);
my $max = min($r1_hi, $r2_hi);
return if $min > $max;
return {min => $min, max => $max};
}
Prints:
$VAR1 = {
'min' => 31,
'max' => 50
};
| [reply] [d/l] [select] |
Re: Calculate the overlap length between two ranges
by Lennotoecom (Pilgrim) on Oct 31, 2013 at 00:27 UTC
|
"the length between some overlapping numerical ranges"
31-52 and 27-50 is it 27 - 52 which is 25 or 31 - 50 which is 19 ?
the task is not clear anyway:
$a = '31-52 27-50'; @a = sort split /\s|-/, $a;
print $a[3] - $a[0]," or ",$a[2] - $a[1],"\n";
| [reply] [d/l] |
Re: Calculate the overlap length between two ranges
by locked_user sundialsvc4 (Abbot) on Oct 30, 2013 at 14:19 UTC
|
Two ranges A and B overlap if low(A) <= high(B) AND high(A) >= low(B) ... hope I got that right, but it’s something like that. Yes, there should be a CPAN module as-noted which does this simple task and probably others that will be of interest to you.
Then, again IIRC, the distance ought to be something like Max(Low(A), High(B)) - Min(Low(B), High(A)) ... not so sure about that one, it’s been a while. Anyway, I do remember that you are testing the opposite-end numbers in each pair in some way.
| |
|
|
Except that the OP did not speak about distance but about "the length between some overlapping numerical ranges", this phrasing does not really make sense to me. The length between overlapping ranges, if anything, is 0. This is why I kindly asked for clarification. Or, perhaps, it is a deficiency in my command of the English language. But the OP answered some later posts by other monks, but did not see it fit to answer mine.
| [reply] |