How to resolve the algorithm Range expansion step by step in the Delphi programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Range expansion step by step in the Delphi 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).

Expand the range description: Note that the second element above, is the range from minus 3 to minus 1.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Range expansion step by step in the Delphi programming language

Source code in the delphi programming language

program Range_expansion;

{$APPTYPE CONSOLE}

uses
  System.SysUtils;

const
  input = '-6,-3--1,3-5,7-11,14,15,17-20';

var
  r: TArray;
  last, i, n: Integer;

begin
  for var part in input.Split([',']) do
  begin
    i := part.Substring(1).IndexOf('-');
    if i = -1 then
    begin
      if not TryStrToInt(part, n) then
        raise Exception.Create('Error: value invalid ' + part);
      if Length(r) > 0 then
        if last = n then
        begin
          raise Exception.Create('Error: Duplicate value:' + n.ToString);
        end
        else
        begin
          if last > n then
            raise Exception.CreateFmt('Error: values not ordered: %s > %s', [last, n]);
        end;
      SetLength(r, Length(r) + 1);
      r[High(r)] := n;
      last := n;
    end
    else
    begin
      var n1 := 0;
      var n2 := 0;
      if not TryStrToInt(part.Substring(0, i + 1), n1) then
        raise Exception.Create('Error: value invalid ' + part);
      if not TryStrToInt(part.Substring(i + 2), n2) then
        raise Exception.Create('Error: value invalid ' + part);
      if n2 < n1 + 2 then
        raise Exception.Create('Error: Invalid range' + part);
      if Length(r) > 0 then
      begin
        if last = n1 then
          raise Exception.Create('Error: Duplicate value: ' + n1.ToString);
        if last > n1 then
          raise Exception.CreateFmt('Error: Values not ordened: %d > %d', [last, n1]);
      end;

      for i := n1 to n2 do
      begin
        SetLength(r, Length(r) + 1);
        r[High(r)] := i;
      end;

      last := n2;
    end;
  end;
  write('expanded: ');
  for var rr in r do
  begin
    write(rr, ',');
  end;
  readln;
end.


  

You may also check:How to resolve the algorithm Read a file line by line step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Topic variable step by step in the Oforth programming language
You may also check:How to resolve the algorithm Fibonacci n-step number sequences step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Create an HTML table step by step in the Tcl programming language
You may also check:How to resolve the algorithm Modular arithmetic step by step in the Fortran programming language