Order two numerical lists: Difference between revisions

m
m (syntax highlighting fixup automation)
 
(5 intermediate revisions by 5 users not shown)
Line 1,057:
FALSE
TRUE</pre>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
func listcmp a[] b[] .
for i to lower len a[] len b[]
if a[i] < b[i]
return -1
elif a[i] > b[i]
return 1
.
.
if len a[] < len b[]
return -1
elif len a[] > len b[]
return 1
.
return 0
.
print listcmp [ 2 4 5 ] [ 2 3 1 ]
print listcmp [ 2 3 1 ] [ 2 3 1 ]
print listcmp [ 2 3 1 ] [ 2 3 1 3 ]
</syntaxhighlight>
 
=={{header|Ela}}==
Line 1,066 ⟶ 1,089:
 
[1,2,1,3,2] <. [1,2,0,4,4,0,0,0]</syntaxhighlight>
 
 
=={{header|Elixir}}==
Line 1,719 ⟶ 1,741:
io.write_string(" : "), io.write_string(S), io.nl,
{ A < B -> S = "yes" ; S = "no" }.</syntaxhighlight>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (lay (map show [
order [1,2,1,3,2] [1,2,0,4,4,0,0,0],
order [1,2,0,4,4,0,0,0] [1,2,1,3,2]]))]
 
order :: [*]->[*]->bool
order as [] = True
order [] as = False
order (a:as) (b:bs) = a < b, if a ~= b
= order as bs, otherwise</syntaxhighlight>
{{out}}
<pre>False
True</pre>
 
=={{header|Nim}}==
Line 2,241 ⟶ 2,278:
list2>list3
list3=list4
</pre>
 
=={{header|RPL}}==
{{works with|HP|28}}
{| class="wikitable" ≪
! RPL code
! Comment
|-
|
≪ → a b
≪ a SIZE b SIZE MIN LAST <
1 ROT '''FOR''' j
'''IF''' b j GET a j GET - '''THEN'''
LAST 0 > SWAP DROP
a SIZE 'j' STO
'''END'''
'''NEXT'''
≫ ≫ ‘<span style="color:blue">GR8R?</span>’ STO
|
<span style="color:blue">GR8R?</span> ''( [reals] [reals] → boolean ) ''
m = min(size(a),size(b)) ; res = (size(a)<size(b))
loop for j=1 to m
if b[j]-a[j] ≠ 0
res = (b[j]-a[j]>0)
break
end if
end loop, return res
.
|}
[1 2 1 5 2] [1 2 1 5 2 2] <span style="color:blue">GR8R?</span>
[1 2 1 5 2 2] [1 2 1 5 2] <span style="color:blue">GR8R?</span>
[1 2 1 5 2 2] [1 2 3 4 5] <span style="color:blue">GR8R?</span>
[1 2 3 4 5] [1 2 1 5 2 2] <span style="color:blue">GR8R?</span>
[1 2 3 4 5] [1 2 3 4 5] <span style="color:blue">GR8R?</span>
{{out}}
<pre>
5: 1
4: 0
3: 1
2: 0
1: 0
</pre>
 
Line 2,506 ⟶ 2,584:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var orderLists = Fn.new { |l1, l2|
var len = (l1.count <= l2.count) ? l1.count : l2.count
for (i in 0...len) {
Line 2,553 ⟶ 2,631:
</pre>
 
=={{header|XPL0}}==
{{trans|ALGOL W}}
<syntaxhighlight lang "XPL0"> \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</syntaxhighlight>
{{out}}
<pre>
List1 < List2
List2 < List3
List3 >= List4
List5 >= List6
List6 >= List7
List7 >= List8
List8 < List7
</pre>
 
=={{header|Yabasic}}==
Line 2,588 ⟶ 2,737:
Igual que la entrada de BBC BASIC.
</pre>
 
 
=={{header|zkl}}==
2,054

edits