Largest int from concatenated ints: Difference between revisions
Content added Content deleted
(→{{header|Julia}}: A new entry for Julia) |
|||
Line 635: | Line 635: | ||
| quicksort( .[0] + .[1] < .[1] + .[0] ) |
| quicksort( .[0] + .[1] < .[1] + .[0] ) |
||
| reverse | join("") ;</lang> |
| reverse | join("") ;</lang> |
||
=={{header|Julia}}== |
|||
Perhaps algorithm 3 is more efficient, but algorithm 2 is decent and very easy to implement in Julia. So this solution uses algorithm 2. |
|||
<lang Julia> |
|||
function maxconcat{T<:Integer}(a::Array{T,1}) |
|||
b = map(string, a) |
|||
b = sort(b, lt=(x,y)->x*y < y*x, rev=true) |
|||
b = join(b, "") |
|||
try |
|||
b = parseint(b) |
|||
catch |
|||
b = parseint(BigInt, b) |
|||
end |
|||
end |
|||
tests = {[1, 34, 3, 98, 9, 76, 45, 4], |
|||
[54, 546, 548, 60], |
|||
[1, 34, 3, 98, 9, 76, 45, 4, 54, 546, 548, 60]} |
|||
for t in tests |
|||
println("Maxconcating ", t) |
|||
println(" ", maxconcat(t)) |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
Maxconcating [1,34,3,98,9,76,45,4] |
|||
998764543431 |
|||
Maxconcating [54,546,548,60] |
|||
6054854654 |
|||
Maxconcating [1,34,3,98,9,76,45,4,54,546,548,60] |
|||
9987660548546544543431 |
|||
</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |