Jump to content

Range consolidation: Difference between revisions

Added Wren
m (Improved comment for Rust code)
(Added Wren)
Line 1,704:
 
test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
As Wren already has a built-in Range class (which is not quite the same as what's required here), we create a Span class instead.
<lang ecmascript>import "/math" for Math
 
class Span {
construct new(r) {
if (r.type != Range || !r.isInclusive) Fiber.abort("Argument must be an inclusive range.")
_low = r.from
_high = r.to
if (_low > _high) {
_low = r.to
_high = r.from
}
}
 
low { _low }
high { _high }
 
consolidate(r) {
if (r.type != Span) Fiber.abort("Argument must be a Span.")
if (_high < r.low) return [this, r]
if (r.high < _low) return [r, this]
return [Span.new(Math.min(_low, r.low)..Math.max(_high, r.high))]
}
 
toString { "[%(_low), %(_high)]" }
}
 
var spanLists = [
[Span.new(1.1..2.2)],
[Span.new(6.1..7.2), Span.new(7.2..8.3)],
[Span.new(4..3), Span.new(2..1)],
[Span.new(4..3), Span.new(2..1), Span.new(-1..-2), Span.new(3.9..10)],
[Span.new(1..3), Span.new(-6..-1), Span.new(-4..-5), Span.new(8..2), Span.new(-6..-6)]
]
 
for (spanList in spanLists) {
if (spanList.count == 1) {
System.print(spanList.toString[1..-2])
} else if (spanList.count == 2) {
System.print(spanList[0].consolidate(spanList[1]).toString[1..-2])
} else {
var first = 0
while (first < spanList.count-1) {
var next = first + 1
while (next < spanList.count) {
var res = spanList[first].consolidate(spanList[next])
spanList[first] = res[0]
if (res.count == 2) {
spanList[next] = res[1]
next = next + 1
} else {
spanList.removeAt(next)
}
}
first = first + 1
}
System.print(spanList.toString[1..-2])
}
}</lang>
 
{{out}}
<pre>
[1.1, 2.2]
[6.1, 8.3]
[1, 2], [3, 4]
[-2, -1], [1, 2], [3, 10]
[-6, -1], [1, 8]
</pre>
 
9,482

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.