How to resolve the algorithm Range consolidation step by step in the Julia programming language
How to resolve the algorithm Range consolidation step by step in the Julia 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 Julia programming language
The first function, normalize
, takes a list of ranges and sorts the ranges by their lower bounds.
The second function, consolidate
, takes a list of ranges and consolidates them into a list of non-overlapping ranges.
It does this by first normalizing the list of ranges, and then iterating over the normalized list of ranges.
For each range r1
in the normalized list, consolidate
iterates over the remaining ranges in the normalized list and checks if r1
intersects any of them.
If r1
intersects another range r2
, consolidate
merges r1
and r2
into a new range and empties r2
.
After iterating over all the ranges in the normalized list, consolidate
returns a list of the non-overlapping ranges.
The third function, testranges
, tests the consolidate
function by applying it to a list of ranges and printing the results.
The output of testranges
is as follows:
[[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, 4]]
[[4, 3], [2, 1], [-1, -2], [3.9, 10]] => [[-1, -2], [1, 4], [3.9, 10]]
[[1, 3], [-6, -1], [-4, -5], [8, 2], [-6, -6]] => [[-6, -6], [-4, -5], [-1, -1], [1, 3], [8, 8]]
Source code in the julia programming language
normalize(s) = sort([sort(bounds) for bounds in s])
function consolidate(ranges)
norm = normalize(ranges)
for (i, r1) in enumerate(norm)
if !isempty(r1)
for r2 in norm[i+1:end]
if !isempty(r2) && r1[end] >= r2[1] # intersect?
r1 .= [r1[1], max(r1[end], r2[end])]
empty!(r2)
end
end
end
end
[r for r in norm if !isempty(r)]
end
function testranges()
for s in [[[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]]]
println("$s => $(consolidate(s))")
end
end
testranges()
You may also check:How to resolve the algorithm Verify distribution uniformity/Naive step by step in the Haskell programming language
You may also check:How to resolve the algorithm Generate Chess960 starting position step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Evolutionary algorithm step by step in the Oforth programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Logo programming language
You may also check:How to resolve the algorithm Record sound step by step in the AutoHotkey programming language