user
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:

:   :   :   :   :   :   :   :   :   **[b0, b1]**
                            :   or equally as:

                                :   **[b1, b0]**

Given two ranges, the act of consolidation between them compares the two ranges:

* If one range covers all of the other then the result is that encompassing range.
* If the ranges touch or intersect then the result is   *one*   new single range covering the overlapping ranges.
* Otherwise the act of consolidation is to return the two non-touching 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.

Example 1
:   Given the two ranges   **[1, 2.5]**   and   **[3, 4.2]**   then
:   there is no common region between the ranges and the result is the same as the input.

Example 2
:   Given the two ranges   **[1, 2.5]**   and   **[1.8, 4.7]**   then
:   there is :   an overlap   **[2.5, 1.8]**   between the ranges and
:   the result is the single range   **[1, 4.7]**.
:   Note that order of bounds in a range is not (yet) stated.

Example 3
:   Given the two ranges   **[6.1, 7.2]**   and   **[7.2, 8.3]**   then
:   they touch at   **7.2**   and
:   the result is the single range   **[6.1, 8.3]**.

Example 4
:   Given the three ranges   **[1, 2]**   and   **[4, 8]**   and   **[2, 5]**
:   then there is no intersection of the ranges   **[1, 2]**   and   **[4, 8]**
:   but the ranges   **[1, 2]**   and   **[2, 5]**   overlap and
:   consolidate to produce the range   **[1, 5]**.
:   This range, in turn, overlaps the other range   **[4, 8]**,   and
:   so consolidates to the final output of the single range   **[1, 8]**.

Task

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:

```
           [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]  

```

Show all output here.

See also

* Set consolidation
* Set of real numbers
assistant
(formerly Perl 6)

**Works with**: Rakudo version 2020.08.1

In Raku, a Range is a first class object with its own specialized notation. Raku Ranges allow for exclusion of the boundary numbers. This example doesn't since it isn't a requirement in this task. Much of the logic is lifted from the Set\_of\_real\_numbers task with simplified logic for the much simpler requirements.

Note: the output is in standard Raku notation for Ranges.

```
# 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]) }
}

```

Output:

```
                                 [[1.1, 2.2],] => (1.1..2.2)
                      [[6.1, 7.2], [7.2, 8.3]] => (6.1..8.3)
                              [[4, 3], [2, 1]] => (1..2 3..4)
         [[4, 3], [2, 1], [-1, -2], [3.9, 10]] => (-2..-1 1..2 3..10)
[[1, 3], [-6, -1], [-4, -5], [8, 2], [-6, -6]] => (-6..-1 1..8)

```