Largest int from concatenated ints: Difference between revisions

Content added Content deleted
(<lang julia></lang>)
Line 1,148: Line 1,148:
=={{header|Julia}}==
=={{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.
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})
<lang julia>function maxconcat(arr::Vector{<:Integer})
b = sort(dec.(arr); lt=(x, y) -> x * y < y * x, rev=true) |> join
b = map(string, a)
b = sort(b, lt=(x,y)->x*y < y*x, rev=true)
return try parse(Int, b) catch parse(BigInt, b) end
b = join(b, "")
try
b = parseint(b)
catch
b = parseint(BigInt, b)
end
end
end


tests = {[1, 34, 3, 98, 9, 76, 45, 4],
tests = ([1, 34, 3, 98, 9, 76, 45, 4],
[54, 546, 548, 60],
[54, 546, 548, 60],
[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
for arr in tests
println("Maxconcating ", t)
println("Max concatenating in $arr:\n -> ", maxconcat(arr))
end</lang>
println(" ", maxconcat(t))
</lang>


{{out}}
{{out}}
<pre>Max concatenating in [1, 34, 3, 98, 9, 76, 45, 4]:
<pre>
-> 998764543431
Maxconcating [1,34,3,98,9,76,45,4]
Max concatenating in [54, 546, 548, 60]:
998764543431
-> 6054854654
Maxconcating [54,546,548,60]
Max concatenating in [1, 34, 3, 98, 9, 76, 45, 4, 54, 546, 548, 60]:
6054854654
-> 9987660548546544543431</pre>
Maxconcating [1,34,3,98,9,76,45,4,54,546,548,60]
9987660548546544543431
</pre>


=={{header|Kotlin}}==
=={{header|Kotlin}}==