How to resolve the algorithm Range extraction step by step in the Seed7 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Range extraction step by step in the Seed7 programming language

Table of Contents

Problem Statement

A format for expressing an ordered list of integers is to use a comma separated list of either Example The list of integers: Is accurately expressed by the range expression: (And vice-versa).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Range extraction step by step in the Seed7 programming language

Source code in the seed7 programming language

$ include "seed7_05.s7i";

const func string: rangeExtraction (in array integer: numbers) is func
  result
    var string: rangeStri is "";
  local
    var integer: index is 1;
    var integer: index2 is 1;
  begin
    while index <= length(numbers) do
      while index2 <= pred(length(numbers)) and numbers[succ(index2)] = succ(numbers[index2]) do
        incr(index2);
      end while;
      if succ(index) < index2 then
        rangeStri &:= "," <& numbers[index] <& "-" <& numbers[index2];
      else
        while index <= index2 do
          rangeStri &:= "," <& numbers[index];
          incr(index);
	end while;
      end if;
      incr(index2);
      index := index2;
    end while;
    rangeStri := rangeStri[2 ..];
  end func;

const proc: main is func
  begin
    writeln(rangeExtraction([] (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)));
  end func;

  

You may also check:How to resolve the algorithm Arrays step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Variables step by step in the Set lang programming language
You may also check:How to resolve the algorithm Flipping bits game step by step in the Perl programming language
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Knuth shuffle step by step in the uBasic/4tH programming language