How to resolve the algorithm Range consolidation step by step in the Go programming language
How to resolve the algorithm Range consolidation step by step in the Go 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 Go programming language
Package-Level Declarations
- The
consolidate
function takes a slice ofRange
structs as input and returns a consolidated slice of ranges. - The
Range
type represents a closed interval on the real number line. - The
Norm
method onRange
normalizes the range such thatLower <= Upper
. - The
String
method onRange
returns a string representation of the range. - The
Union
method onRange
returns a slice of ranges representing the union of two ranges.
consolidate
Function
- The
consolidate
function has the following behavior:- It normalizes all ranges in the input slice.
- It sorts the ranges by their lower bounds.
- It iterates through the sorted ranges and performs the following steps:
- If a range intersects with a subsequent range, it merges the two ranges.
- If a range does not intersect with any subsequent range, it appends the range to the output slice.
- The function returns the output slice.
main
Function
- The
main
function declares a slice of slice of rangesrss
and iterates through it. - For each slice of ranges
rs
, it prints the ranges before and after consolidation.
Example Usage
- The following example shows how to use the
consolidate
function:
package main
import (
"fmt"
"github.com/wei-tao/intervals/range"
)
func main() {
rs := []range.Range{
{1.1, 2.2},
{6.1, 7.2},
{7.2, 8.3},
}
rsConsolidated := range.Consolidate(rs)
fmt.Printf("Consolidated ranges: %v\n", rsConsolidated)
}
- Output:
Consolidated ranges: [[1.1, 8.3]]
Source code in the go programming language
package main
import (
"fmt"
"math"
"sort"
)
type Range struct{ Lower, Upper float64 }
func (r Range) Norm() Range {
if r.Lower > r.Upper {
return Range{r.Upper, r.Lower}
}
return r
}
func (r Range) String() string {
return fmt.Sprintf("[%g, %g]", r.Lower, r.Upper)
}
func (r1 Range) Union(r2 Range) []Range {
if r1.Upper < r2.Lower {
return []Range{r1, r2}
}
r := Range{r1.Lower, math.Max(r1.Upper, r2.Upper)}
return []Range{r}
}
func consolidate(rs []Range) []Range {
for i := range rs {
rs[i] = rs[i].Norm()
}
le := len(rs)
if le < 2 {
return rs
}
sort.Slice(rs, func(i, j int) bool {
return rs[i].Lower < rs[j].Lower
})
if le == 2 {
return rs[0].Union(rs[1])
}
for i := 0; i < le-1; i++ {
for j := i + 1; j < le; j++ {
ru := rs[i].Union(rs[j])
if len(ru) == 1 {
rs[i] = ru[0]
copy(rs[j:], rs[j+1:])
rs = rs[:le-1]
le--
i--
break
}
}
}
return rs
}
func main() {
rss := [][]Range{
{{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}},
}
for _, rs := range rss {
s := fmt.Sprintf("%v", rs)
fmt.Printf("%40s => ", s[1:len(s)-1])
rs2 := consolidate(rs)
s = fmt.Sprintf("%v", rs2)
fmt.Println(s[1 : len(s)-1])
}
}
You may also check:How to resolve the algorithm Logical operations step by step in the OpenEdge/Progress programming language
You may also check:How to resolve the algorithm Respond to an unknown method call step by step in the Ruby programming language
You may also check:How to resolve the algorithm Calculating the value of e step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Loops/While step by step in the Phixmonti programming language
You may also check:How to resolve the algorithm Draw a clock step by step in the Locomotive Basic programming language