The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,842 other followers

I’m looking for an algorithm that converts a list of integers (for instance of…

Posted by jpluimers on 2016/09/28

Got a very quick response Range combo – Paste.ie – Irish For Pastebin on:

I’m looking for an algorithm that converts a list of integers (for instance of pages to be printed) from this form:1,2,3,4,6,7,8,13,14into1-4,6-8,13-14… – Jeroen Wiert Pluimers – Google+

I’ll write some unit tests soon and make it into a unit for testing.

For now it looks exactly what I need. In addition, I learned how this algorithm is called Range extraction – Rosetta Code and the opposite Range expansion – Rosetta Code.

Basically these lists are the format where a user can enter the range of pages to be printed.

The code by Asbjørn Heid uses a Functional record definition allowing for function binding just like in C++ Boost.Bind [WayBack].

Based on that, I made this changeset: https://bitbucket.org/jeroenp/besharp.net/commits/7205b070a4e6457675a083b78d26659da506fc08

–jeroen

via: I’m looking for an algorithm that converts a list of integers (for instance of…

4 Responses to “I’m looking for an algorithm that converts a list of integers (for instance of…”

  1. Jens01 said

    unit tools.ParseNumbers.Test;

    interface

    uses
    TestFramework, tools.ParseNumbers;

    type

    TestParseNumbers = class(TTestCase)
    private
    FParseNumbers: TParseNumbers;
    procedure CheckNumberlist(const Expected, Actual: array of Integer);
    procedure SetUp; override;
    procedure TearDown; override;
    published
    procedure TestCreateNumberString;
    procedure TestCreateNumberList;
    procedure TestIsStringValid;
    end;

    implementation

    { TestParseNumbers }

    procedure TestParseNumbers.CheckNumberlist(const Expected, Actual: array of Integer);
    var
    i: Integer;
    begin
    CheckEquals(Length(Expected), Length(Actual));
    for i := 0 to Length(Expected) – 1 do
    CheckEquals(Expected[i], Actual[i]);
    end;

    procedure TestParseNumbers.SetUp;
    begin
    inherited;
    FParseNumbers := TParseNumbers.Create;
    end;

    procedure TestParseNumbers.TearDown;
    begin
    FParseNumbers.Free;
    inherited;
    end;

    procedure TestParseNumbers.TestCreateNumberList;
    var
    Res : TArray;
    Res1 : Boolean;
    begin
    Res1 := FParseNumbers.CreateNumberList(‘1,2,3’, Res);
    CheckTrue(Res1);
    CheckNumberlist([1, 2, 3], Res);
    Res1 := FParseNumbers.CreateNumberList(‘3,2,1’, Res);
    CheckTrue(Res1);
    CheckNumberlist([1, 2, 3], Res);

    Res1 := FParseNumbers.CreateNumberList(‘1,1,1’, Res);
    CheckTrue(Res1);
    CheckNumberlist([1], Res);
    Res1 := FParseNumbers.CreateNumberList(‘1,1,1’, Res, False);
    CheckTrue(Res1);
    CheckNumberlist([1, 1, 1], Res);
    Res1 := FParseNumbers.CreateNumberList(‘1,3’, Res);
    CheckTrue(Res1);
    CheckNumberlist([1, 3], Res);
    Res1 := FParseNumbers.CreateNumberList(‘3,1’, Res);
    CheckTrue(Res1);
    CheckNumberlist([1, 3], Res);

    Res1 := FParseNumbers.CreateNumberList(‘1-3’, Res);
    CheckTrue(Res1);
    CheckNumberlist([1, 2, 3], Res);
    Res1 := FParseNumbers.CreateNumberList(‘1-3,5’, Res);
    CheckTrue(Res1);
    CheckNumberlist([1, 2, 3, 5], Res);
    Res1 := FParseNumbers.CreateNumberList(‘3-1,5’, Res);
    CheckTrue(Res1);
    CheckNumberlist([1, 2, 3, 5], Res);
    Res1 := FParseNumbers.CreateNumberList(‘5,1-3’, Res);
    CheckTrue(Res1);
    CheckNumberlist([1, 2, 3, 5], Res);
    Res1 := FParseNumbers.CreateNumberList(‘5,3-1’, Res);
    CheckTrue(Res1);
    CheckNumberlist([1, 2, 3, 5], Res);
    Res1 := FParseNumbers.CreateNumberList(‘1-3,1-3’, Res);
    CheckTrue(Res1);
    CheckNumberlist([1, 2, 3], Res);
    Res1 := FParseNumbers.CreateNumberList(‘1-3,1-3’, Res, False);
    CheckTrue(Res1);
    CheckNumberlist([1, 1, 2, 2, 3, 3], Res);

    Res1 := FParseNumbers.CreateNumberList(‘5 1’, Res);
    CheckTrue(Res1);
    CheckNumberlist([1, 5], Res);
    end;

    procedure TestParseNumbers.TestCreateNumberString;
    var
    Res : string;
    Expected: string;
    begin
    Res := FParseNumbers.CreateNumberString([1, 2, 3]);
    Expected := ‘1-3’;
    CheckEquals(Expected, Res);
    Res := FParseNumbers.CreateNumberString([3, 2, 1]);
    Expected := ‘1-3’;
    CheckEquals(Expected, Res);

    Res := FParseNumbers.CreateNumberString([1, 1, 2, 2, 3, 3]);
    Expected := ‘1,1,2,2,3,3’;
    CheckEquals(Expected, Res);

    Res := FParseNumbers.CreateNumberString([5, 1, 2, 3]);
    Expected := ‘1-3,5’;
    CheckEquals(Expected, Res);
    Res := FParseNumbers.CreateNumberString([5, 3, 2, 1]);
    Expected := ‘1-3,5’;
    CheckEquals(Expected, Res);
    end;

    procedure TestParseNumbers.TestIsStringValid;
    begin
    CheckTrue(FParseNumbers.IsStringValid(‘1,2,3’));
    CheckTrue(FParseNumbers.IsStringValid(‘3,2,1’));
    CheckTrue(FParseNumbers.IsStringValid(‘1,1,1’));
    CheckTrue(FParseNumbers.IsStringValid(‘1-3’));
    CheckTrue(FParseNumbers.IsStringValid(‘1-3,5’));
    CheckTrue(FParseNumbers.IsStringValid(‘3-1,5’));
    CheckTrue(FParseNumbers.IsStringValid(‘5,1-3’));
    CheckTrue(FParseNumbers.IsStringValid(‘5,3-1’));
    CheckTrue(FParseNumbers.IsStringValid(‘5 1’));
    CheckFalse(FParseNumbers.IsStringValid(‘5a’));
    end;

    initialization

    RegisterTest(TestParseNumbers.Suite);

    end.

  2. Jens01 said

    I did it some years ago:
    http://www.delphipraxis.net/1178632-post10.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: