How to resolve the algorithm Order two numerical lists step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Order two numerical lists step by step in the XPL0 programming language

Table of Contents

Problem Statement

Write a function that orders two lists or arrays filled with numbers. The function should accept two lists as arguments and return true if the first list should be ordered before the second, and false otherwise. The order is determined by lexicographic order: Comparing the first element of each list. If the first elements are equal, then the second elements should be compared, and so on, until one of the list has no more elements. If the first list runs out of elements the result is true. If the second list or both run out of elements the result is false. Note: further clarification of lexicographical ordering is expounded on the talk page here and here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Order two numerical lists step by step in the XPL0 programming language

Source code in the xpl0 programming language

    \Compare lists (rows) of integers.
    \Returns TRUE if there is an element in A that is < the corresponding
    \ element in B and all previous elements are equal, FALSE otherwise.
    \The bounds of A and B should ALB :: AUB and BLB :: BUB.
    function ILT ( A, ALB, AUB, B, BLB, BUB );
    integer  A, ALB, AUB, B, BLB, BUB;
    integer  APos, BPos, Equal;
    begin
        APos := ALB;
        BPos := BLB;
        Equal := true;
        while APos <= AUB and BPos <= BUB and Equal do begin
            Equal := A( APos ) = B( BPos );
            if Equal then begin
                APos := APos + 1;
                BPos := BPos + 1
           end \if_Equal
        end; \while_more_elements_and_Equal
        if not Equal
        then \there is an element in A and B that is not Equal
             return A( APos ) < B( BPos )
        else \all elements are Equal or one list is shorter
             \A is < B if A has fewer elements
             return APos > AUB and BPos <= BUB
    end; \ILT

    \Tests A < B has the expected result
    procedure Test ( AName, A, ALB, AUB, BName, B, BLB, BUB, Expected );
    integer   AName, A, ALB, AUB, BName, B, BLB, BUB, Expected;
    integer   IsLt;
    begin
        IsLt := ILT( A, ALB, AUB, B, BLB, BUB );
        Text(0, AName);
        Text(0, if IsLt then " <  " else " >= ");
        Text(0, BName);
        Text(0, if IsLt = Expected then " " else ", NOT as expected");
        CrLf(0);
    end; \test

    integer List1, List2, List3, List4, List5, List6, List7, List8;
begin 
    \test cases as in the BBC basic sample
    List1 := [0, 1, 2, 1, 5, 2];
    List2 := [0, 1, 2, 1, 5, 2, 2];
    List3 := [0, 1, 2, 3, 4, 5];
    List4 := [0, 1, 2, 3, 4, 5];
    Test( "List1", List1, 1, 5, "List2", List2, 1, 6, true  );
    Test( "List2", List2, 1, 6, "List3", List3, 1, 5, true  );
    Test( "List3", List3, 1, 5, "List4", List4, 1, 5, false );
    \additional test cases
    List5 := [0, 9, 0, 2, 1, 0];
    List6 := [0, 4, 0, 7, 7];
    List7 := [0, 4, 0, 7];
    List8 := [0, 0];
    Test( "List5", List5, 1, 5, "List6", List6, 1, 4, false );
    Test( "List6", List6, 1, 4, "List7", List7, 1, 3, false );
    Test( "List7", List7, 1, 3, "List8", List8, 1, 0, false );
    Test( "List8", List8, 1, 0, "List7", List7, 1, 3, true  )
end

  

You may also check:How to resolve the algorithm Canonicalize CIDR step by step in the 11l programming language
You may also check:How to resolve the algorithm Factorial step by step in the SparForte programming language
You may also check:How to resolve the algorithm Host introspection step by step in the Scala programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the LiveCode programming language
You may also check:How to resolve the algorithm String comparison step by step in the ALGOL 68 programming language