How to resolve the algorithm Range consolidation step by step in the Factor programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Range consolidation step by step in the Factor 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 Factor programming language

Source code in the factor programming language

USING: arrays combinators formatting kernel math.combinatorics
math.order math.statistics sequences sets sorting ;

: overlaps? ( pair pair -- ? )
    2dup swap [ [ first2 between? ] curry any? ] 2bi@ or ;

: merge ( seq -- newseq ) concat minmax 2array 1array ;

: merge1 ( seq -- newseq )
    dup 2 [ first2 overlaps? ] find-combination
    [ [ without ] keep merge append ] when* ;

: normalize ( seq -- newseq ) [ natural-sort ] map ;

: consolidate ( seq -- newseq )
    normalize [ merge1 ] to-fixed-point natural-sort ;

{
    { { 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 } }
} [ dup consolidate "%49u => %u\n" printf ] each


  

You may also check:How to resolve the algorithm Assertions step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the Clojure programming language
You may also check:How to resolve the algorithm Array length step by step in the Dart programming language
You may also check:How to resolve the algorithm Sierpinski triangle/Graphical step by step in the Objeck programming language
You may also check:How to resolve the algorithm Generic swap step by step in the Frink programming language