Hello Monks! I have been given the following assignment, and I am not entirely sure I understand what I need to do. It says:
Given two ranges, do the following:
1) check if the start or the end of each range deviate by more than
+five
2) check if the intersection of the range is at least half its union
I made the following code, that solves part 1 for two sample ranges:
use Set::IntSpan;
$range1 = Set::IntSpan->new([45 .. 60]);
$range2 = Set::IntSpan->new([49 .. 57]);
$intersect_range = $range1->intersect($range2);
$union_range = $range1->union($range2);
print "INTERSECT:".$intersect_range."\nUNION:".$union_range."\n";
$diff_between_starts = abs($range1->min-$range2->min);
$diff_between_ends = abs($range1->max-$range2->max);
print "START DIFF:$diff_between_starts\n";
print "END DIFF:$diff_between_ends\n";
if($diff_between_starts<=5 && $diff_between_ends<=5) {print "ALL IS OK
+\n";}
else {print "PROBLEM\n";}
My problem is I don't understand what they want in (2). Do they refer to the LENGTH of the intersection being at least half the length of the union?