59 lines
2.2 KiB
Text
59 lines
2.2 KiB
Text
\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
|