Range extraction: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added PicoLisp)
(→‎{{header|J}}: Added C++)
Line 318: Line 318:
(0:2, 4, 6:8, 11:12, 14:25, 27:33, 35:39)
(0:2, 4, 6:8, 11:12, 14:25, 27:33, 35:39)
</pre>
</pre>

=={{header|C++}}==

<lang c++>
#include <iostream>
#include <iterator>
#include <cstddef>

template<typename InIter>
void extract_ranges(InIter begin, InIter end, std::ostream& os)
{
if (begin == end)
return;

int current = *begin++;
os << current;
int count = 1;

while (begin != end)
{
int next = *begin++;
if (next == current+1)
++count;
else
{
if (count > 2)
os << '-';
else
os << ',';
if (count > 1)
os << current << ',';
os << next;
count = 1;
}
current = next;
}

if (count > 1)
os << (count > 2? '-' : ',') << current;
}

template<typename T, std::size_t n>
T* end(T (&array)[n])
{
return array+n;
}

int main()
{
int data[] = { 0, 1, 2, 4, 6, 7, 8, 11, 12, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
37, 38, 39 };

extract_ranges(data, end(data), std::cout);
std::cout << std::endl;
}
</lang>



=={{header|J}}==
=={{header|J}}==

Revision as of 11:12, 17 July 2010

Task
Range extraction
You are encouraged to solve this task according to the task description, using any language you may know.

A format for expressing an ordered list of integers is to use a comma separated list of either

  • individual integers
  • Or a range of integers denoted by the starting integer separated from the end integer in the range by a dash, '-'. (The range includes all integers in the interval including both endpoints)
  • The range syntax is to be used only for, and for every range that expands to more than two values.

Example
The list of integers:

-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20

Is accurately expressed by the range expression:

-6,-3-1,3-5,7-11,14,15,17-20

(And vice-versa).

The task

  • Create a function that takes a list of integers and returns a correctly formatted string in the range format.
  • Use the function to compute and print the range formatted version of the following ordered list of integers:
    0,  1,  2,  4,  6,  7,  8, 11, 12, 14,
   15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
   25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
   37, 38, 39

C.f. Range expansion

ALGOL 68

This example is incorrect. Please fix the code and remove this message.

Details: Range format is with a dash and no spaces, plus ranges must cover >2 integers, thanks.

Note: The following Iterative code specimen is the "unrolled" version of the Generative code specimen below. Together they provided as a comparison of the two different methods.

Iterative

Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny
  • The closest concept that Algol 68 has to duck typing is the tagged union. This is used to define mode rangeint = union(int, struct(int lwb, upb)). If duck typing was available it could reduced the size of the code specimen, but would have lost some of Algol 68's strong type data security.

<lang algol68>MODE INTLIST = FLEX[0]INT;

  1. Declarations for manipulating lists of range pairs [lwb:upb] #

MODE RANGE = STRUCT(INT lwb, upb); MODE RANGELIST = FLEX[0]RANGE;

PROC range repr = (RANGE range)STRING:

 whole(lwb OF range,0)+
   IF lwb OF range = upb OF range THEN "" ELSE ":"+whole(upb OF range,0) FI;
  1. OP REPR = (RANGE range)STRING: range repr(range); firmly related to RANGEINT #
  1. Declarations for manipulating lists containing pairs AND lone INTs #

MODE RANGEINT = UNION(INT, RANGE); MODE RANGEINTLIST = FLEX[0]RANGEINT;

PROC range int repr = (RANGEINT range int)STRING:

 CASE range int IN
   (RANGE range): range repr(range),
   (INT int): whole(int,0)
 ESAC;

OP REPR = (RANGEINT range int)STRING: range int repr(range int);

  1. The closest thing ALGOL 68 has to inheritance is the union #

MODE RANGEINTLISTINIT = UNION(RANGEINTLIST, RANGELIST, INTLIST);

PROC range int list repr = (RANGEINTLIST range int list)STRING: (

 STRING out := "(", sep := "";
 FOR key FROM LWB range int list TO UPB range int list DO
   out +:= sep + REPR range int list[key];
   sep := ", "
 OD;
 out+")"

);

OP REPR = (RANGEINTLIST range int list)STRING: range int list repr(range int list);

  1. Task portion #

PROC range int list init = (RANGEINTLISTINIT range int list)RANGEINTLIST: (

 [LWB range int list: UPB range int list]RANGEINT out range int list;
 INT upb out range int list := LWB out range int list - 1;
 UNION(VOID, RANGE) prev range := EMPTY;
 PROC out range int list append = (RANGE value)VOID:
   out range int list[upb out range int list+:=1] :=
     IF lwb OF value = upb OF value THEN lwb OF value ELSE value FI;
  1. Note: Algol 68RS cannot handle LWB and UPB of a UNION in the following: #
 FOR key FROM LWB range int list TO UPB range int list DO
   RANGEINT value = CASE range int list IN 
                      (INTLIST list):list[key],
                      (RANGELIST list):list[key],
                      (RANGEINTLIST list):list[key]
                    ESAC;
   RANGE next range := CASE value IN
       (RANGE range): range,
       (INT value): RANGE(value, value)
     ESAC;
   prev range := 
     CASE prev range IN
       (VOID): next range,
       (RANGE prev range): 
         IF upb OF prev range + 1 = lwb OF next range THEN
           RANGE(lwb OF prev range, upb OF next range) # merge the range #
         ELSE
           IF lwb OF prev range <= upb OF prev range THEN
             out range int list append(prev range)
           FI;
           next range
         FI
       OUT SKIP
     ESAC
 OD;
 CASE prev range IN 
   (RANGE last range): out range int list append(last range)
 ESAC;
 out range int list[:upb out range int list]

);

  1. do some simple test cases: #

test: BEGIN

 []INT int list = (
   0,  1,  2,  4,  6,  7,  8, 11, 12, 14,
   15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
   25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
   37, 38, 39);
 
 []RANGE range list = ( # unnormalised #
   (0,0),  (1,1),  (2,2),  (4,4),  (6,6),  (7,7),  (8,8), (11,11), (12,12), (14,14),
   (15,15), (16,16), (17,17), (18,18), (19,19), (20,20), (21,21), (22,22), (23,23), (24,24),
   (25,25), (27,27), (28,28), (29,29), (30,30), (31,31), (32,32), (33,33), (35,35), (36,36),
   (37,37), (38,38), (39,39));
 
 []RANGEINT list a = (RANGE(0,2), 4, RANGE(6,8), RANGE(11,12), RANGE(14,25), RANGE(27,33), RANGE(35,39));
 []RANGEINT list b = ( # unnormalised #
   0,  1,  2,  4,  6,  7,  8, 11, 12, 14,
   15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
   25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
   37, 38, 39);
 FLEX[0]RANGEINT list c := range int list init(list b); # normalised #
 
  1. compare manipulation of various types of argument lists #
 print((REPR range int list init(int list), new line));
 print((REPR range int list init(range list), new line));
 print((REPR list a, new line));
 print((REPR(range int list init(list b)), new line));
 print((REPR list c, new line))

END</lang> Output:

(0:2, 4, 6:8, 11:12, 14:25, 27:33, 35:39)
(0:2, 4, 6:8, 11:12, 14:25, 27:33, 35:39)
(0:2, 4, 6:8, 11:12, 14:25, 27:33, 35:39)
(0:2, 4, 6:8, 11:12, 14:25, 27:33, 35:39)
(0:2, 4, 6:8, 11:12, 14:25, 27:33, 35:39)

Generative

Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny
  • The following code a set of helper functions/generators that can be used to manipulate a lists of ranges. They can manipulate either arrays or iterator. And they can handle data of type int or range and both these types unioned.

These chained iterators do the following steps:

  1. Iterate through three different types of initial arrays - []int, []range and []rangeint with gen range, yielding range(lwb,upb)
  2. Iterate with gen range merge yielding merged range(lwb,upb)
  3. Iterate with gen range int merge, merging and yielding a union of int and range
  4. Finally iterate with range int list init exiting with an array of union of int and range.

<lang algol68>MODE INTLIST = FLEX[0]INT; MODE YIELDINT = PROC(INT)VOID;

  1. Declarations for manipulating lists of range pairs [lwb:upb] #

MODE RANGE = STRUCT(INT lwb, upb); MODE RANGELIST = FLEX[0]RANGE; MODE YIELDRANGE = PROC(RANGE)VOID;

PROC range repr = (RANGE range)STRING:

 whole(lwb OF range,0)+
   IF lwb OF range = upb OF range THEN "" ELSE ":"+whole(upb OF range,0) FI;
  1. OP REPR = (RANGE range)STRING: range repr(range); firmly related to RANGEINT #
  1. Declarations for manipulating lists containing pairs AND lone INTs #

MODE RANGEINT = UNION(INT, RANGE); MODE RANGEINTLIST = FLEX[0]RANGEINT; MODE YIELDRANGEINT = PROC(RANGEINT)VOID;

PROC range int repr = (RANGEINT range int)STRING:

 CASE range int IN
   (RANGE range): range repr(range),
   (INT int): whole(int,0)
 ESAC;

OP REPR = (RANGEINT range int)STRING: range int repr(range int);

  1. The closest thing ALGOL 68 has to inheritance is the union #

MODE RANGEINTLISTINIT = UNION(RANGEINTLIST, RANGELIST, INTLIST);

PROC range int list repr = (RANGEINTLIST range int list)STRING: (

 STRING out := "(", sep := "";
 FOR key FROM LWB range int list TO UPB range int list DO
   out +:= sep + REPR range int list[key];
   sep := ", "
 OD;
 out+")"

);

OP REPR = (RANGEINTLIST range int list)STRING: range int list repr(range int list);

  1. Note: Algol 68RS cannot handle LWB and UPB of a UNION in the following: #

PROC gen range = (RANGEINTLISTINIT range int list, YIELDRANGE yield range)VOID:

 FOR key FROM LWB range int list TO UPB range int list DO
   RANGEINT value = CASE range int list IN 
                      (INTLIST list):list[key],
                      (RANGELIST list):list[key],
                      (RANGEINTLIST list):list[key]
                    ESAC;
   yield range(
     CASE value IN
       (RANGE range): range,
       (INT value): (value, value)
     ESAC
   )
 OD;

PROC gen range merge = (RANGEINTLISTINIT range int list, YIELDRANGE yield range)VOID: (

 UNION(VOID, RANGE) prev range := EMPTY;
  1. FOR RANGE next range IN # gen range(range int list, # ) DO #
    1. (RANGE next range)VOID:
  2. if the ranges cannot be merge, then yield 1st, and return 2nd #
   prev range := 
     CASE prev range IN
       (VOID): next range,
       (RANGE prev range): 
         IF upb OF prev range + 1 = lwb OF next range THEN
           RANGE(lwb OF prev range, upb OF next range) # merge the range #
         ELSE
           IF lwb OF prev range <= upb OF prev range THEN
             yield range(prev range)
           FI;
           next range
         FI
       OUT SKIP
     ESAC
  1. OD # );
 CASE prev range IN (RANGE last range): yield range(last range) ESAC

);

PROC gen range int merge = (RANGEINTLISTINIT range int list, YIELDRANGEINT yield range int)VOID: (

  1. FOR RANGE range IN # gen range merge(range int list, # ) DO #
    1. (RANGE range)VOID:
   yield range int(
     IF lwb OF range = upb OF range THEN lwb OF range ELSE range FI
   )
  1. OD # )

);

PROC range int list init = (RANGEINTLISTINIT range int list)RANGEINTLIST: (

 [LWB range int list: UPB range int list]RANGEINT out range int list;
 INT upb out range int list := LWB out range int list - 1;
  1. FOR RANGEINT range int IN # gen range int merge(range int list, # ) DO #
    1. (RANGEINT range int)VOID:
   out range int list[upb out range int list+:=1] := range int
  1. OD # );
 out range int list[:upb out range int list]

);

  1. do some simple test cases: #

test: BEGIN

 []INT int list = (
   0,  1,  2,  4,  6,  7,  8, 11, 12, 14,
   15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
   25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
   37, 38, 39);
 
 []RANGE range list = ( # unnormalised #
   (0,0),  (1,1),  (2,2),  (4,4),  (6,6),  (7,7),  (8,8), (11,11), (12,12), (14,14),
   (15,15), (16,16), (17,17), (18,18), (19,19), (20,20), (21,21), (22,22), (23,23), (24,24),
   (25,25), (27,27), (28,28), (29,29), (30,30), (31,31), (32,32), (33,33), (35,35), (36,36),
   (37,37), (38,38), (39,39));
 
 []RANGEINT list a = (RANGE(0,2), 4, RANGE(6,8), RANGE(11,12), RANGE(14,25), RANGE(27,33), RANGE(35,39));
 []RANGEINT list b = ( # unnormalised #
   0,  1,  2,  4,  6,  7,  8, 11, 12, 14,
   15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
   25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
   37, 38, 39);
 FLEX[0]RANGEINT list c := range int list init(list b); # normalised #
 
  1. compare manipulation of various types of argument lists #
 print((REPR range int list init(int list), new line));
 print((REPR range int list init(range list), new line));
 print((REPR list a, new line));
 print((REPR(range int list init(list b)), new line));
 print((REPR list c, new line))

END</lang> Output:

(0:2, 4, 6:8, 11:12, 14:25, 27:33, 35:39)
(0:2, 4, 6:8, 11:12, 14:25, 27:33, 35:39)
(0:2, 4, 6:8, 11:12, 14:25, 27:33, 35:39)
(0:2, 4, 6:8, 11:12, 14:25, 27:33, 35:39)
(0:2, 4, 6:8, 11:12, 14:25, 27:33, 35:39)

C++

<lang c++>

  1. include <iostream>
  2. include <iterator>
  3. include <cstddef>

template<typename InIter>

void extract_ranges(InIter begin, InIter end, std::ostream& os)

{

 if (begin == end)
   return;
 int current = *begin++;
 os << current;
 int count = 1;
 while (begin != end)
 {
   int next = *begin++;
   if (next == current+1)
     ++count;
   else
   {
     if (count > 2)
       os << '-';
     else
       os << ',';
     if (count > 1)
       os << current << ',';
     os << next;
     count = 1;
   }
   current = next;
 }
 if (count > 1)
   os << (count > 2? '-' : ',') << current;

}

template<typename T, std::size_t n>

T* end(T (&array)[n])

{

 return array+n;

}

int main() {

 int data[] = { 0,  1,  2,  4,  6,  7,  8, 11, 12, 14,
                15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
                25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
                37, 38, 39 };
 extract_ranges(data, end(data), std::cout);
 std::cout << std::endl;

} </lang>


J

<lang j>require 'strings' fmt=: [`":`(":@{. , (',-' {~ 2 < #) , ":@{:)@.(2 <. #) group=: <@fmt;.1~ 1 ~: 0 , 2 -~/\ ] extractRange=: ',' joinstring group</lang>

Example use:

<lang j>raw=:". -.&LF 0 :0

   0,  1,  2,  4,  6,  7,  8, 11, 12, 14,
  15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
  37, 38, 39

)

  extractRange raw

0-2,4,6-8,11,12,14-25,27-33,35-39</lang>

MUMPS

This example is incorrect. Please fix the code and remove this message.

Details: Ranges must cover >2 integers, thanks.

<lang MUMPS> RANGCONT(X) ;Integer range contraction

NEW Y,I,CONT,FIRST,CURR,PREV,SEQ SET Y="",SEQ=0,PREV="",CONT=0
FOR I=1:1:$LENGTH(X,",") DO
.S FIRST=$L(Y),CURR=$P(X,",",I)
.FOR  Q:$EXTRACT(CURR)'=" "  S CURR=$EXTRACT(CURR,2,$LENGTH(CURR))  ;clean up leading spaces
.S SEQ=((CURR-1)=PREV)
.IF 'FIRST SET Y=CURR
.IF FIRST DO
..;Order matters due to flags
..IF CONT&SEQ ;Do nothing
..IF 'CONT&'SEQ SET Y=Y_","_CURR
..IF CONT&'SEQ SET Y=Y_PREV_","_CURR,CONT=0
..IF 'CONT&SEQ SET Y=Y_"-",CONT=1
.SET PREV=CURR
IF CONT SET Y=Y_PREV
K I,CONT,FIRST,CURR,PREV,SEQ
QUIT Y</lang>

Example:

USER>SET S="0,1,2,4,6,7,8,11,12,14,15,16,17,18,19,20,21,22,23,24,25,27,28,29,30,31,32,33,35,36,37,38,39"
 
USER>W $$RANGCONT^ROSETTA(S)
0-2,4,6-8,11-12,14-25,27-33,35-39

OCaml

<lang ocaml>let range_extract = function

 | [] -> []
 | x::xs ->
     let rec aux acc = function
       | (a,b), c::tail ->
           if (succ b) = c
           then aux acc ((a,c), tail)
           else aux ((a,b)::acc) ((c,c), tail)
       | v, [] ->
           List.rev (v::acc)
     in
     aux [] ((x,x), xs)

let string_of_range rng =

 let rec aux acc = function
   | (a,b)::tl ->
       let this =
         if a = b
         then (string_of_int a)
         else (Printf.sprintf "%d-%d" a b)
       in
       aux (this::acc) tl
   | [] ->
       String.concat "," (List.rev acc)
 in
 aux [] rng

let () =

 let li =
   [ 0; 1; 2; 4; 6; 7; 8; 11; 12; 14; 15; 16; 17; 18; 19; 20; 21;
     22; 23; 24; 25; 27; 28; 29; 30; 31; 32; 33; 35; 36; 37; 38; 39 ]
 in
 let rng = range_extract li in
 print_endline(string_of_range rng)</lang>

Oz

<lang oz>declare

 fun {Extract Xs}
    {CommaSeparated
     {Map {ExtractRanges Xs} RangeToString}}
 end
 fun {ExtractRanges Xs}
    fun {Loop Ys Start End}
       case Ys
       of Y|Yr andthen Y == End+1 then {Loop Yr Start Y}
       [] Y|Yr                    then Start#End|{Loop Yr Y Y} 
       [] nil                     then [Start#End]
       end
    end
 in
    case Xs
    of X|Xr then {Loop Xr X X}
    [] nil then nil
    end
 end
 
 fun {RangeToString S#E}
    if E-S >= 2 then
       {VirtualString.toString S#"-"#E}
    else
       {CommaSeparated
        {Map {List.number S E 1} Int.toString}}
    end
 end
 fun {CommaSeparated Xs}
    {Flatten {Intersperse "," Xs}}
 end
  
 fun {Intersperse Sep Xs}
    case Xs of X|Y|Xr then
       X|Sep|{Intersperse Sep Y|Xr}
    else
       Xs
    end
 end

in

 {System.showInfo
  {Extract [ 0 1 2 4 6 7 8 11 12 14
             15 16 17 18 19 20 21 22 23 24
             25 27 28 29 30 31 32 33 35 36
             37 38 39 ]}}</lang>

PicoLisp

<lang PicoLisp>(de rangeextract (Lst)

  (glue ","
     (make
        (while Lst
           (let (N (pop 'Lst)  M N)
              (while (= (inc M) (car Lst))
                 (setq M (pop 'Lst)) )
              (cond
                 ((= N M) (link N))
                 ((= (inc N) M) (link N M))
                 (T (link (list N '- M))) ) ) ) ) ) )</lang>

Output:

: (rangeextract
   (0 1 2 4 6 7 8 11 12 14 15 16 17 18 19 20 21 22
      23 24 25 27 28 29 30 31 32 33 35 36 37 38 39 ) )

-> "0-2,4,6-8,11,12,14-25,27-33,35-39"

Python

<lang python>#import random

def rangeextract(lst):

   lenlst = len(lst)
   i, ranges = 0, []
   while i< lenlst:
       low = lst[i]
       while i <lenlst-1 and lst[i]+1 == lst[i+1]: i +=1
       hi = lst[i]
       ranges.append(
           '%i-%i'  % (low, hi) if hi - low >= 2 else
           ('%i,%i' % (low, hi) if hi - low == 1 else
            '%i' % low) )
       i += 1
   return ','.join(ranges)
  1. lst = sorted(random.sample(list(range(40)), 33))
  2. print (lst)

lst = [ 0, 1, 2, 4, 6, 7, 8, 11, 12, 14,

      15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
      25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
      37, 38, 39]

print(rangeextract(lst))</lang>

Sample output

0-2,4,6-8,11,12,14-25,27-33,35-39

Ruby

<lang ruby>def range_extract(l)

 sorted = l.sort
 range = []
 start = sorted.first
 # pad the list with a big value, so that the last loop iteration will 
 # appended something to the range
 sorted.concat([Float::MAX]).each_cons(2) do |prev,n|
   if prev.succ < n
     if start == prev
       range << start.to_s
     else
       range << "%d%s%d" % [start, (start.succ == prev ? "," : "-"), prev]
     end
     start = n
   end
 end
 range.join(',')

end

lst = [

   0,  1,  2,  4,  6,  7,  8, 11, 12, 14,
  15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
  37, 38, 39

]

p rng = range_extract(lst)</lang>

output:

"0-2,4,6-8,11,12,14-25,27-33,35-39"

Tcl

<lang tcl>proc rangeExtract list {

   set result [lindex $list 0]
   set first [set last [lindex $list 0]]
   foreach term [lrange $list 1 end] {

if {$term == $last+1} { set last $term continue } if {$last > $first} { append result [expr {$last == $first+1 ? "," : "-"}] $last } append result "," $term set first [set last $term]

   }
   if {$last == $first+1} {

append result "," $last

   } elseif {$last > $first} {

append result "-" $last

   }
   return $result

}

  1. Commas already removed so it is a natural Tcl list

puts [rangeExtract {

   0 1 2 4 6 7 8 11 12 14
   15 16 17 18 19 20 21 22 23 24
   25 27 28 29 30 31 32 33 35 36
   37 38 39

}]</lang> Output:

0-2,4,6-8,11,12,14-25,27-33,35-39