Order two numerical lists: Difference between revisions

Line 699:
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
<tt>islexfirst</tt> is a somewhat permissive function in that it will accept many sorts of lists for comparison. It does check that all of the elements of both input lists are of some real number type, and if not will throw a <tt>DomainError</tt>.
 
<lang julia>function islexless(a::AbstractArray{<:Real}, b::AbstractArray{<:Real})
'''Functions'''
for (x, y) in zip(a, b)
<lang Julia>
if x == y continue end
function isallreal{T<:AbstractArray}(a::T)
all(map(x->isa(x, Real), a))
end
 
function islexfirst{T<:AbstractArray,U<:AbstractArray}(a::T, b::U)
isallreal(a) && isallreal(b) || throw(DomainError())
for i in 1:min(length(a), length(b))
x = a[i]
y = b[i]
x != y || continue
return x < y
end
return length(a) < length(b)
end
</lang>
 
using Primes, Combinatorics
'''Main'''
tests = [[1, 2, 3], primes(10), 0:2:6, [-Inf, 0.0, Inf], [π, e, φ, catalan], [2015, 5], [-sqrt(50.0), 50.0 ^ 2]]
<lang Julia>
println("List not sorted:\n - ", join(tests, "\n - "))
tests = {[1, 2, 3],
sort!(tests; lt=islexless)
primes(10),
println("List sorted:\n - ", join(tests, "\n - "))</lang>
0:2:6,
[-Inf, 0.0, Inf],
[π, e, φ, catalan],
[2015, 5],
[-sqrt(50.0), 50.0^2],
}
 
println("Testing islexfirst:")
for (a, b) in combinations(tests, 2)
tres = islexfirst(a, b) ? " is " : " is not "
tres *= "lexically prior to\n "
println("\n ", a, tres, b)
end
</lang>
 
{{out}}
<pre>List not sorted:
- [1, 2, 3]
Testing islexfirst:
- [2, 3, 5, 7]
 
- 0:2:6
[1,2,3] is lexically prior to
- [2-Inf,3 0.0,5,7 Inf]
- [3.14159, 2.71828, 1.61803, 0.915966]
 
- [2015, 5]
[1,2,3] is not lexically prior to
- [-7.07107, 2500.0]
0:2:6
List sorted:
 
- [-Inf, 0.0, Inf]
[1,2,3] is not lexically prior to
- [-Inf7.07107,0 2500.0,Inf]
- 0:2:6
 
- [1, 2, 3] is lexically prior to
- [2, 3, 5, 7]
[3.141592653589793,2.718281828459045,1.618033988749895,0.915965594177219]
- [3.14159, 2.71828, 1.61803, 0.915966]
 
- [2015, 5]</pre>
[1,2,3] is lexically prior to
[2015,5]
 
[1,2,3] is not lexically prior to
[-7.0710678118654755,2500.0]
 
[2,3,5,7] is not lexically prior to
0:2:6
 
[2,3,5,7] is not lexically prior to
[-Inf,0.0,Inf]
 
[2,3,5,7] is lexically prior to
[3.141592653589793,2.718281828459045,1.618033988749895,0.915965594177219]
 
[2,3,5,7] is lexically prior to
[2015,5]
 
[2,3,5,7] is not lexically prior to
[-7.0710678118654755,2500.0]
 
0:2:6 is not lexically prior to
[-Inf,0.0,Inf]
 
0:2:6 is lexically prior to
[3.141592653589793,2.718281828459045,1.618033988749895,0.915965594177219]
 
0:2:6 is lexically prior to
[2015,5]
 
0:2:6 is not lexically prior to
[-7.0710678118654755,2500.0]
 
[-Inf,0.0,Inf] is lexically prior to
[3.141592653589793,2.718281828459045,1.618033988749895,0.915965594177219]
 
[-Inf,0.0,Inf] is lexically prior to
[2015,5]
 
[-Inf,0.0,Inf] is lexically prior to
[-7.0710678118654755,2500.0]
 
[3.141592653589793,2.718281828459045,1.618033988749895,0.915965594177219] is lexically prior to
[2015,5]
 
[3.141592653589793,2.718281828459045,1.618033988749895,0.915965594177219] is not lexically prior to
[-7.0710678118654755,2500.0]
 
[2015,5] is not lexically prior to
[-7.0710678118654755,2500.0]
</pre>
 
=={{header|Kotlin}}==
Anonymous user