How to resolve the algorithm Range consolidation step by step in the Raku programming language
How to resolve the algorithm Range consolidation step by step in the Raku programming language
Table of Contents
Problem Statement
Define a range of numbers R, with bounds b0 and b1 covering all numbers between and including both bounds.
That range can be shown as:
Given two ranges, the act of consolidation between them compares the two ranges:
Given N ranges where N > 2 then the result is the same as repeatedly replacing all combinations of two ranges by their consolidation until no further consolidation between range pairs is possible. If N < 2 then range consolidation has no strict meaning and the input can be returned.
Let a normalized range display show the smaller bound to the left; and show the range with the smaller lower bound to the left of other ranges when showing multiple ranges. Output the normalized result of applying consolidation to these five sets of ranges: Show all output here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Range consolidation step by step in the Raku programming language
Source code in the raku programming language
# Union
sub infix:<∪> (Range $a, Range $b) { Range.new($a.min,max($a.max,$b.max)) }
# Intersection
sub infix:<∩> (Range $a, Range $b) { so $a.max >= $b.min }
multi consolidate() { () }
multi consolidate($this is copy, **@those) {
gather {
for consolidate |@those -> $that {
if $this ∩ $that { $this ∪= $that }
else { take $that }
}
take $this;
}
}
for [[1.1, 2.2],],
[[6.1, 7.2], [7.2, 8.3]],
[[4, 3], [2, 1]],
[[4, 3], [2, 1], [-1, -2], [3.9, 10]],
[[1, 3], [-6, -1], [-4, -5], [8, 2], [-6, -6]]
-> @intervals {
printf "%46s => ", @intervals.raku;
say reverse consolidate |@intervals.grep(*.elems)».sort.sort({ [.[0], .[*-1]] }).map: { Range.new(.[0], .[*-1]) }
}
You may also check:How to resolve the algorithm Averages/Median step by step in the Nim programming language
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the Swift programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the Chapel programming language
You may also check:How to resolve the algorithm Classes step by step in the Object Pascal programming language
You may also check:How to resolve the algorithm Fractran step by step in the CLU programming language