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