Range consolidation: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 69:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F consolidate(ranges)
F normalize(s)
R sorted(s.filter(bounds -> !bounds.empty).map(bounds -> sorted(bounds)))
Line 89:
[[4.0, 3.0], [2.0, 1.0], [-1.0, -2.0], [3.9, 10.0]],
[[1.0, 3.0], [-6.0, -1.0], [-4.0, -5.0], [8.0, 2.0], [-6.0, -6.0]]]
print(String(s)[1 .< (len)-1]‘ => ’String(consolidate(s))[1 .< (len)-1])</langsyntaxhighlight>
 
{{out}}
Line 103:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:REALMATH.ACT"
 
DEFINE PTR="CARD"
Line 275:
PutE() PutE()
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Range_consolidation.png Screenshot from Atari 8-bit computer]
Line 291:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Ada.Containers.Vectors;
 
Line 377:
Show (Set_4);
Show (Set_5);
end Range_Consolidation;</langsyntaxhighlight>
 
{{out}}
Line 389:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">RangeConsolidation(arr){
arr1 := [], arr2 := [], result := []
Line 406:
result[i-1, 2] := stop > result[i-1, 2] ? stop : result[i-1, 2]
return result
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">test1 := [[1.1, 2.2]]
test2 := [[6.1, 7.2], [7.2, 8.3]]
test3 := [[4, 3], [2, 1]]
Line 422:
}
MsgBox % result
return</langsyntaxhighlight>
{{out}}
<pre>[1.1, 2.2]
Line 431:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 520:
test_consolidate_ranges(test5, LENGTHOF(test5));
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 533:
=={{header|C sharp}}==
{{works with|C sharp|7}}
<langsyntaxhighlight lang="csharp">using static System.Math;
using System.Linq;
using System;
Line 571:
private static (double s, double e) Normalize((double s, double e) range) =>
(Min(range.s, range.e), Max(range.s, range.e));
}</langsyntaxhighlight>
{{out}}
<pre>
Line 581:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <utility>
Line 650:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 663:
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(defn normalize [r]
(let [[n1 n2] r]
[(min n1 n2) (max n1 n2)]))
Line 688:
(remove #(contains? (set used) %) rs))]
(recur (conj res (consolidate-touching-ranges touching))
(remove-used (rest rs) touching))))))</langsyntaxhighlight>
 
{{out}}
Line 710:
{{trans|C#}}
 
<langsyntaxhighlight lang="dyalect">type Pt(s, e) with Lookup
func Pt.Min() => min(this.s, this.e)
Line 749:
list.Sort((x,y) => x.s - y.s)
print(list)
}</langsyntaxhighlight>
 
{{out}}
Line 761:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: arrays combinators formatting kernel math.combinatorics
math.order math.statistics sequences sets sorting ;
 
Line 784:
{ { 4 3 } { 2 1 } { -1 -2 } { 3.9 10 } }
{ { 1 3 } { -6 -1 } { -4 -5 } { 8 2 } { -6 -6 } }
} [ dup consolidate "%49u => %u\n" printf ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 796:
=={{header|FreeBASIC}}==
{{trans|Yabasic}}
<langsyntaxhighlight lang="freebasic">
Dim Shared As Integer i
Dim Shared As Single items, temp = 10^30
Line 868:
Next j
Sleep
</syntaxhighlight>
</lang>
 
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 946:
fmt.Println(s[1 : len(s)-1])
}
}</langsyntaxhighlight>
 
{{out}}
Line 958:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (intercalate, maximumBy, sort)
import Data.Ord (comparing)
 
Line 1,042:
showNum n
| 0 == (n - fromIntegral (round n)) = show (round n)
| otherwise = show n</langsyntaxhighlight>
{{Out}}
<pre>Range consolidations:
Line 1,054:
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">ensure2D=: ,:^:(1 = #@$) NB. if list make 1 row table
normalise=: ([: /:~ /:~"1)@ensure2D NB. normalises list of ranges
merge=: ,:`(<.&{. , >.&{:)@.(>:/&{: |.) NB. merge ranges x and y
consolidate=: (}.@] ,~ (merge {.)) ensure2D</langsyntaxhighlight>
'''Required Examples:'''
<langsyntaxhighlight lang="j"> tests=: <@".;._2 noun define
1.1 2.2
6.1 7.2 ,: 7.2 8.3
Line 1,072:
| | |3 4| 1 2| 1 8|
| | | | 3 10| |
+-------+-------+---+-----+-----+</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.Arrays;
Line 1,185:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,203:
{{Trans|Haskell}}
{{Trans|Python}}
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,379:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Range consolidations:
Line 1,392:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq">def normalize: map(sort) | sort;
def consolidate:
Line 1,420:
;
testranges</langsyntaxhighlight>
{{out}}
<pre>
Line 1,437:
of the Python code is done, rather than using a native Julia Range.
{{trans|Python}}
<langsyntaxhighlight lang="julia">normalize(s) = sort([sort(bounds) for bounds in s])
 
function consolidate(ranges)
Line 1,463:
 
testranges()
</langsyntaxhighlight>{{output}}
<pre>
Array{Float64,1}[[1.1, 2.2]] => Array{Float64,1}[[1.1, 2.2]]
Line 1,473:
 
=={{header|Kotlin}}==
<langsyntaxhighlight Kotlinlang="kotlin">fun <T> consolidate(ranges: Iterable<ClosedRange<T>>): List<ClosedRange<T>> where T : Comparable<T>
{
return ranges
Line 1,538:
 
inputRanges.associateBy(Any::toString, ::consolidateDoubleRanges).forEach({ println("${it.key} => ${it.value}") })
}</langsyntaxhighlight>
 
{{Out}}
Line 1,551:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Using the Wolfram Language's built-in Interval operations:
<langsyntaxhighlight Mathematicalang="mathematica">data={{{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}}};
Column[IntervalUnion@@@Map[Interval,data,{2}]]</langsyntaxhighlight>
{{out}}
<pre>Interval[{1.1,2.2}]
Line 1,565:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import algorithm, strutils
 
# Definition of a range of values of type T.
Line 1,619:
test([4, 3], [2, 1])
test([4.0, 3.0], [2.0, 1.0], [-1.0, -2.0], [3.9, 10.0])
test([1, 3], [-6, -1], [-4, -5], [8, 2], [-6, -6])</langsyntaxhighlight>
 
{{out}}
Line 1,632:
Note: the output is shown in the standard [https://perldoc.perl.org/perlop.html#Range-Operators Perl notation for Ranges].
 
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 1,662:
$out .= join('..', @$_). ' ' for consolidate($intervals);
printf "%44s => %s\n", $in, $out;
}</langsyntaxhighlight>
{{out}}
<pre> [1.1, 2.2] => 1.1..2.2
Line 1,671:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">consolidate</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">sets</span><span style="color: #0000FF;">)</span>
Line 1,706:
<span style="color: #000000;">test</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3.9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">}})</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{-</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{-</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{-</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">6</span><span style="color: #0000FF;">}})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,718:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">consolidate_ranges(Ranges, Consolidated):-
normalize(Ranges, Normalized),
sort(Normalized, Sorted),
Line 1,760:
(consolidate_ranges(Ranges, Consolidated),
write_ranges(Ranges), write(' -> '),
write_ranges(Consolidated), nl)).</langsyntaxhighlight>
 
{{out}}
Line 1,773:
=={{header|Python}}==
===Procedural===
<langsyntaxhighlight lang="python">def normalize(s):
return sorted(sorted(bounds) for bounds in s if bounds)
 
Line 1,795:
]:
print(f"{str(s)[1:-1]} => {str(consolidate(s))[1:-1]}")
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,810:
{{Trans|Haskell}}
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Range consolidation'''
 
from functools import reduce
Line 1,897:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Consolidation of numeric ranges:
Line 1,908:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
;; Racket's max and min allow inexact numbers to contaminate exact numbers
Line 1,935:
([1 3] [-6 -1] [-4 -5] [8 2] [-6 -6])))
 
(for ([xs (in-list inputs)]) (printf "~a => ~a\n" xs (solve xs)))</langsyntaxhighlight>
 
{{out}}
Line 1,953:
Note: the output is in standard [https://docs.raku.org/type/Range Raku notation for Ranges].
 
<syntaxhighlight lang="raku" perl6line># Union
sub infix:<∪> (Range $a, Range $b) { Range.new($a.min,max($a.max,$b.max)) }
 
Line 1,979:
printf "%46s => ", @intervals.raku;
say reverse consolidate |@intervals.grep(*.elems)».sort.sort({ [.[0], .[*-1]] }).map: { Range.new(.[0], .[*-1]) }
}</langsyntaxhighlight>
{{out}}
<pre> [[1.1, 2.2],] => (1.1..2.2)
Line 1,992:
 
The actual logic for the range consolidation is marked with the comments: &nbsp; &nbsp; <big> <tt> /*■■■■►*/ </tt> </big>
<langsyntaxhighlight lang="rexx">/*REXX program performs range consolidation (they can be [equal] ascending/descending). */
#.= /*define the default for range sets. */
parse arg #.1 /*obtain optional arguments from the CL*/
Line 2,046:
do k=j+1 to n; if word(@.k,1)>=word(_,1) then iterate; @.j=@.k; @.k=_; _=@.j
end /*k*/
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,074:
The algorithm relies on normalizing the ranges and folding a sorted sequence of them.
 
<langsyntaxhighlight Rustlang="rust">use std::fmt::{Display, Formatter};
 
// We could use std::ops::RangeInclusive, but we would have to extend it to
Line 2,227:
 
println!("Run: cargo test -- --nocapture");
}</langsyntaxhighlight>
 
{{out}}
Line 2,250:
This is not a particularly efficient solution, but it gets the job done.
 
<syntaxhighlight lang="sql">
<lang SQL>
/*
This code is an implementation of "Range consolidation" in SQL ORACLE 19c
Line 2,393:
;
/
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,414:
=={{header|Wren}}==
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.
<langsyntaxhighlight lang="ecmascript">class Span {
construct new(r) {
if (r.type != Range || !r.isInclusive) Fiber.abort("Argument must be an inclusive range.")
Line 2,469:
System.print(spanList.toString[1..-2])
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,481:
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">sub sort(tabla())
local items, i, t1, t2, s
Line 2,551:
for i = 1 to items
if tabla(i, 1) <> void print tabla(i, 1), "..", tabla(i, 2);
next</langsyntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn consolidate(rs){
(s:=List()).append(
normalize(rs).reduce('wrap(ab,cd){
Line 2,561:
}) )
}
fcn normalize(s){ s.apply("sort").sort(fcn(a,b){ a[0]<b[0] }) }</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach rs in (L(
L(L(1.1, 2.2)), L(L(6.1, 7.2), L(7.2, 8.3)), L(L(4, 3), L(2, 1)),
L(L(4.0, 3.0), L(2.0, 1.0), L(-1.0, -2.0), L(3.9, 10.0)),
L(L(1, 3), L(-6, -1), L(-4, -5), L(8, 2), L(-6, -6)),
)){ println(ppp(rs),"--> ",ppp(consolidate(rs))) }
fcn ppp(ll){ ll.pump(String,fcn(list){ list.concat(", ", "[", "] ") }) }</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits