Order two numerical lists: Difference between revisions

m
{{out}}
m ({{out}})
Line 4:
The function should accept two lists as arguments and return <code>true</code> if the first list should be ordered before the second, and <code>false</code> otherwise.
 
The order is determined by [[wp:Lexicographical order#Ordering of sequences of various lengths|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 <code>true</code>. if the second list or both run out of elements the result is <code>false</code>.
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 <code>true</code>.
If the second list or both run out of elements the result is <code>false</code>.
 
=={{header|ACL2}}==
Line 43 ⟶ 46:
end Order;
</lang>
{{out}}
Output:
<pre>
TRUE
Line 90 ⟶ 93:
}
</lang>
{{out}}
<p>output:</p>
<pre>
list1<list2
Line 118 ⟶ 121:
IF list1(i%) > list2(i%) THEN = FALSE
= l1% < l2%</lang>
{{out}}
'''Output:'''
<pre>
list1<list2
Line 126 ⟶ 129:
 
=={{header|Bracmat}}==
When evaluating a sum or a product, Bracmat creates an expression with a canonical order, which happens to be compatible with the order defined in this task. In a pattern, only a sum or product on the left hand side (lhs) of the match (<code>:</code>) operator is evaluated. In the solution below we match a composition of the two function arguments into a sum of two terms with itself. If the match expression succeeds, the lhs must already have been in canonical order before evaluation, which means that the first argument is smaller than the second argument. In that case the function outputs FALSE. Notice that if the arguments are the same, evaluation of the sum produces the product of one of the terms and a factor two. This complicates the pattern a bit.
In a pattern, only a sum or product on the left hand side (lhs) of the match (<code>:</code>) operator is evaluated.
In the solution below we match a composition of the two function arguments into a sum of two terms with itself.
If the match expression succeeds, the lhs must already have been in canonical order before evaluation, which means that the first argument is smaller than the second argument.
In that case the function outputs FALSE.
Notice that if the arguments are the same, evaluation of the sum produces the product of one of the terms and a factor two. This complicates the pattern a bit.
<lang bracmat>( 1 2 3 4 5:?List1
& 1 2 1 5 2 2:?List2
Line 151 ⟶ 159:
& gt$(!List6,!List7)
);</lang>
{{out}}
Output:
<pre>TRUE
TRUE
Line 361 ⟶ 369:
return b
}</lang>
{{out}}
Output:
<pre>
order [0] before [] : false
Line 574 ⟶ 582:
)
#first < #second</lang>
{{out}}
""Output""
<pre>false
true</pre>
Line 590 ⟶ 598:
print :list1 = :list2</lang>
 
{{out}}
output:
<lang logo>true
false
Line 636 ⟶ 644:
end</lang>
 
Output{{out}} (time used as random seed: 1413127434):
<pre>
 
{10, 7, 4, 9, 10, 3, 5, 5, 5, 5} > {7, 4, 6, 4, 3, 5, 10}
{5, 7} <= {6, 3, 7, 7, 7, 1}
Line 648 ⟶ 656:
{4, 3, 4, 6, 3, 6, 7, 2, 2, 5} > {3, 10, 6, 8, 1}
{1, 5, 1, 5, 4} > {1, 3, 5, 3, 2, 10, 1}
</pre>
 
=={{header|Mathematica}}==
Line 743 ⟶ 752:
 
echo([1,2,1,3,2] < [1,2,0,4,4,0,0,0])</lang>
{{out}}
Output:
<pre>false</pre>
 
Line 993 ⟶ 1,002:
EndIf
</lang>
{{out}}
Sample output:
<pre>False
False
Line 1,048 ⟶ 1,057:
if wx<wy then return 'true'
return 'false'</lang>
{{out}}
'''output'''
<pre>
[1 2 1 5 2] < [1 2 1 5 2 2]
Line 1,179 ⟶ 1,188:
ENDLOOP
</lang>
{{out}}
Output:
<pre style='height:30ex;overflow:scroll'>
false: 1'2'5'6'7 < 1'2'1'3'2
Line 1,205 ⟶ 1,214:
(car.a < car.b)</lang>
 
{{out}}
Output:
<pre>(< '(1 2 3) '(1 2 4))
=> 4
Anonymous user