Order two numerical lists: Difference between revisions

Content deleted Content added
m {{out}}
Line 4: 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 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>.
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>.


=={{header|ACL2}}==
=={{header|ACL2}}==
Line 43: Line 46:
end Order;
end Order;
</lang>
</lang>
{{out}}
Output:
<pre>
<pre>
TRUE
TRUE
Line 90: Line 93:
}
}
</lang>
</lang>
{{out}}
<p>output:</p>
<pre>
<pre>
list1<list2
list1<list2
Line 118: Line 121:
IF list1(i%) > list2(i%) THEN = FALSE
IF list1(i%) > list2(i%) THEN = FALSE
= l1% < l2%</lang>
= l1% < l2%</lang>
{{out}}
'''Output:'''
<pre>
<pre>
list1<list2
list1<list2
Line 126: Line 129:


=={{header|Bracmat}}==
=={{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.
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.
<lang bracmat>( 1 2 3 4 5:?List1
<lang bracmat>( 1 2 3 4 5:?List1
& 1 2 1 5 2 2:?List2
& 1 2 1 5 2 2:?List2
Line 151: Line 159:
& gt$(!List6,!List7)
& gt$(!List6,!List7)
);</lang>
);</lang>
{{out}}
Output:
<pre>TRUE
<pre>TRUE
TRUE
TRUE
Line 361: Line 369:
return b
return b
}</lang>
}</lang>
{{out}}
Output:
<pre>
<pre>
order [0] before [] : false
order [0] before [] : false
Line 574: Line 582:
)
)
#first < #second</lang>
#first < #second</lang>
{{out}}
""Output""
<pre>false
<pre>false
true</pre>
true</pre>
Line 590: Line 598:
print :list1 = :list2</lang>
print :list1 = :list2</lang>


{{out}}
output:
<lang logo>true
<lang logo>true
false
false
Line 636: Line 644:
end</lang>
end</lang>


Output (time used as random seed: 1413127434):
{{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}
{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}
{5, 7} <= {6, 3, 7, 7, 7, 1}
Line 648: Line 656:
{4, 3, 4, 6, 3, 6, 7, 2, 2, 5} > {3, 10, 6, 8, 1}
{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}
{1, 5, 1, 5, 4} > {1, 3, 5, 3, 2, 10, 1}
</pre>


=={{header|Mathematica}}==
=={{header|Mathematica}}==
Line 743: Line 752:


echo([1,2,1,3,2] < [1,2,0,4,4,0,0,0])</lang>
echo([1,2,1,3,2] < [1,2,0,4,4,0,0,0])</lang>
{{out}}
Output:
<pre>false</pre>
<pre>false</pre>


Line 993: Line 1,002:
EndIf
EndIf
</lang>
</lang>
{{out}}
Sample output:
<pre>False
<pre>False
False
False
Line 1,048: Line 1,057:
if wx<wy then return 'true'
if wx<wy then return 'true'
return 'false'</lang>
return 'false'</lang>
{{out}}
'''output'''
<pre>
<pre>
[1 2 1 5 2] < [1 2 1 5 2 2]
[1 2 1 5 2] < [1 2 1 5 2 2]
Line 1,179: Line 1,188:
ENDLOOP
ENDLOOP
</lang>
</lang>
{{out}}
Output:
<pre style='height:30ex;overflow:scroll'>
<pre style='height:30ex;overflow:scroll'>
false: 1'2'5'6'7 < 1'2'1'3'2
false: 1'2'5'6'7 < 1'2'1'3'2
Line 1,205: Line 1,214:
(car.a < car.b)</lang>
(car.a < car.b)</lang>


{{out}}
Output:
<pre>(< '(1 2 3) '(1 2 4))
<pre>(< '(1 2 3) '(1 2 4))
=> 4
=> 4