Jump to content

Largest int from concatenated ints: Difference between revisions

→‎{{header|Picat}}: Split into subsections. Add {{out}}
(→‎{{header|Picat}}: Split into subsections. Add {{out}})
Line 1,674:
=={{header|Picat}}==
On the simpler cases, four methods are tested: 2 using permutations and 2 with different sorting methods.
 
===permutation/2===
<lang Picat>s_perm1(L, Num) =>
permutation(L,P),
Num = [I.to_string() : I in P].flatten().to_integer().</lang>
 
 
===Using permutations/1===
<lang Picat>s_perm2(L, Num) =>
Perms = permutations(L),
Num = max([ [I.to_string() : I in P].flatten().to_integer() : P in Perms]).</lang>
 
===Sort on concatenated numbers===
<lang Picat>s_sort_conc(L,Num) =>
Num = [to_string(I) : I in qsort(L,f3)].join('').to_integer().
 
% sort function for s_sort_conc/2
f3(N1,N2) =>
N1S = N1.to_string(),
N2S = N2.to_string(),
(N1S ++ N2S).to_integer() >= (N2S ++ N1S).to_integer().
 
% qsort(List, SortFunction)
% returns a sorted list according to the sort function SortFunction.
qsort([],_F) = [].
qsort([H|T],F) = qsort([E : E in T, call(F,E,H)], F)
++ [H] ++
qsort([E : E in T, not call(F,E,H)],F).</lang>
 
===Extend each element to the largest length===
<lang Picat>s_extend(L,Num) =>
LS = [I.to_string() : I in L],
MaxLen = 2*max([I.length : I in LS]),
L2 = [],
foreach(I in LS)
I2 = I,
% extend to a larger length
while(I2.length < MaxLen)
I2 := I2 ++ I
end,
% keep info of the original number
L2 := L2 ++ [[I2,I]]
end,
Num = [I[2] : I in qsort(L2,f4)].join('').to_integer().
 
% sort function for s_extend/2
f4(N1,N2) => N1[1].to_integer() >= N2[1].to_integer().</lang>
 
===Test===
<lang Picat>import util.
 
Line 1,703 ⟶ 1,752:
s_extend(L,Num4),
println(s_extent=Num4),
nl.</lang>
 
 
% Using permutation/2
s_perm1(L, Num) =>
permutation(L,P),
Num = [I.to_string() : I in P].flatten().to_integer().
 
% Using permutations/1
s_perm2(L, Num) =>
Perms = permutations(L),
Num = max([ [I.to_string() : I in P].flatten().to_integer() : P in Perms]).
 
% Sort on concatenated numbers N1N2 <-> N2N1
s_sort_conc(L,Num) =>
Num = [to_string(I) : I in qsort(L,f3)].join('').to_integer().
 
% sort function for s_sort_conc/2
f3(N1,N2) =>
N1S = N1.to_string(),
N2S = N2.to_string(),
(N1S ++ N2S).to_integer() >= (N2S ++ N1S).to_integer().
 
% extend each element to the largest length
s_extend(L,Num) =>
LS = [I.to_string() : I in L],
MaxLen = 2*max([I.length : I in LS]),
L2 = [],
foreach(I in LS)
I2 = I,
% extend to a larger length
while(I2.length < MaxLen)
I2 := I2 ++ I
end,
% keep info of the original number
L2 := L2 ++ [[I2,I]]
end,
Num = [I[2] : I in qsort(L2,f4)].join('').to_integer().
 
% sort function for s_extend/2
f4(N1,N2) => N1[1].to_integer() >= N2[1].to_integer().
 
% qsort(List, SortFunction)
% returns a sorted list according to the sort function SortFunction.
qsort([],_F) = [].
qsort([H|T],F) = qsort([E : E in T, call(F,E,H)], F)
++ [H] ++
qsort([E : E in T, not call(F,E,H)],F).</lang>
 
{{out}}
Output:
<pre>l = [1,34,3,98,9,76,45,4]
s_perm1 = 998764543431
Line 1,777 ⟶ 1,779:
s_extend = 9955171</pre>
 
===Testing larger instance===
 
Test a larger instance: 2000 random numbers between 1 and 100; about 5800 digits. The two permutation variants (<code>s_perm1</code> and <code>s_perm2</code>) takes too long on larger N, say N > 9.
<lang Picat>go2 =>
garbage_collect(100_000_000),
Line 1,797 ⟶ 1,799:
nl.</lang>
 
{{out}}
Sample run:
<pre>nums = 2000
str_len = 5805
Line 1,808 ⟶ 1,810:
 
CPU time 0.526 seconds.</pre>
 
 
=={{header|PicoLisp}}==
495

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.