Largest int from concatenated ints: Difference between revisions
No edit summary |
Added Uiua solution |
||
(138 intermediate revisions by 51 users not shown) | |||
Line 1: | Line 1: | ||
{{task}} |
{{task}} |
||
Given a set of positive integers, the task is to write a function to order the integers in such a way that the concatenation of the numbers forms the largest possible integer and return this integer. |
|||
;Task: |
|||
Use the following two sets of integers as tests and show your program output here. |
|||
Given a set of positive integers, write a function to order the integers in such a way that the concatenation of the numbers forms the largest possible integer and return this integer. |
|||
Use the following two sets of integers as tests and show your program output here. |
|||
* {1, 34, 3, 98, 9, 76, 45, 4} |
|||
* {54, 546, 548, 60} |
|||
:::::* {1, 34, 3, 98, 9, 76, 45, 4} |
|||
:::::* {54, 546, 548, 60} |
|||
<br> |
|||
;Possible algorithms: |
;Possible algorithms: |
||
# A solution could be found by trying all combinations and return the best. |
# A solution could be found by trying all combinations and return the best. |
||
# Another way to solve this is to note that in the best arrangement, for any two adjacent original integers X and Y, the concatenation X followed by Y will be numerically greater than or equal to the concatenation Y followed by X. |
# Another way to solve this is to note that in the best arrangement, for any two adjacent original integers '''X''' and '''Y''', the concatenation '''X''' followed by '''Y''' will be numerically greater than or equal to the concatenation '''Y''' followed by '''X. |
||
# Yet another way to solve this is to pad |
# Yet another way to solve this is to pad the integers to the same size by repeating the digits then sort using these repeated integers as a sort key. |
||
Cf: |
|||
;See also: |
|||
* [http://www.quora.com/Algorithms/What-is-the-most-efficient-way-to-arrange-the-given-numbers-to-form-the-biggest-number Algorithms: What is the most efficient way to arrange the given numbers to form the biggest number?]. |
|||
* [http://www.quora.com/Algorithms/What-is-the-most-efficient-way-to-arrange-the-given-numbers-to-form-the-biggest-number Algorithms: What is the most efficient way to arrange the given numbers to form the biggest number?] |
|||
* [http://stackoverflow.com/questions/14532105/constructing-the-largest-number-possible-by-rearranging-a-list/14539943#14539943 Constructing the largest number possible by rearranging a list] |
|||
* [http://stackoverflow.com/questions/14532105/constructing-the-largest-number-possible-by-rearranging-a-list/14539943#14539943 Constructing the largest number possible by rearranging a list] |
|||
<br><br> |
|||
=={{header|11l}}== |
|||
{{trans|Python}} |
|||
<syntaxhighlight lang="11l">F maxnum(x) |
|||
V maxlen = String(max(x)).len |
|||
R sorted(x.map(v -> String(v)), key' i -> i * (@maxlen * 2 I/ i.len), reverse' 1B).join(‘’) |
|||
L(numbers) [[212, 21221], [1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60]] |
|||
print("Numbers: #.\n Largest integer: #15".format(numbers, maxnum(numbers)))</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Numbers: [212, 21221] |
|||
Largest integer: 21221221 |
|||
Numbers: [1, 34, 3, 98, 9, 76, 45, 4] |
|||
Largest integer: 998764543431 |
|||
Numbers: [54, 546, 548, 60] |
|||
Largest integer: 6054854654 |
|||
</pre> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Line 20: | Line 45: | ||
The algorithmic idea is to apply a twisted comparison function: |
The algorithmic idea is to apply a twisted comparison function: |
||
< |
<syntaxhighlight lang="ada">function Order(Left, Right: Natural) return Boolean is |
||
( (Img(Left) & Img(Right)) > (Img(Right) & Img(Left)) );</ |
( (Img(Left) & Img(Right)) > (Img(Right) & Img(Left)) );</syntaxhighlight> |
||
This function converts the parameters Left and Right to strings and returns True if (Left before Right) |
This function converts the parameters Left and Right to strings and returns True if (Left before Right) |
||
exceeds (Right before Left). It needs Ada 2012 -- the code for older versions of Ada would be more verbose. |
exceeds (Right before Left). It needs Ada 2012 -- the code for older versions of Ada would be more verbose. |
||
Line 27: | Line 52: | ||
The rest is straightforward: Run your favourite sorting subprogram that allows to use the function "Order" instead of standard comparison operators ("<" or ">" or so) and print the results: |
The rest is straightforward: Run your favourite sorting subprogram that allows to use the function "Order" instead of standard comparison operators ("<" or ">" or so) and print the results: |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO, Ada.Containers.Generic_Array_Sort; |
||
procedure Largest_Int_From_List is |
procedure Largest_Int_From_List is |
||
Line 58: | Line 83: | ||
Print_Sorted((1, 34, 3, 98, 9, 76, 45, 4)); |
Print_Sorted((1, 34, 3, 98, 9, 76, 45, 4)); |
||
Print_Sorted((54, 546, 548, 60)); |
Print_Sorted((54, 546, 548, 60)); |
||
end Largest_Int_From_List;</ |
end Largest_Int_From_List;</syntaxhighlight> |
||
=={{header|Aime}}== |
|||
<syntaxhighlight lang="aime">largest(...) |
|||
{ |
|||
index x; |
|||
for (, integer e in xcall(list).__list) { |
|||
x[999999999 - 9.times(b_, data(), e).b_size(9).atoi] = e; |
|||
} |
|||
x.ucall(o_, 0); |
|||
o_newline(); |
|||
} |
|||
main(void) |
|||
{ |
|||
largest(1, 34, 3, 98, 9, 76, 45, 4); |
|||
largest(54, 546, 548, 60); |
|||
0; |
|||
}</syntaxhighlight> |
|||
works for input up to 999999999. |
|||
{{Out}} |
|||
<pre>998764543431 |
|||
6054854654</pre> |
|||
=={{header|ALGOL 68}}== |
|||
Using method 2 - first sorting into first digit order and then comparing concatenated pairs. |
|||
<syntaxhighlight lang="algol68">BEGIN |
|||
# returns the integer value of s # |
|||
OP TOINT = ( STRING s)INT: |
|||
BEGIN |
|||
INT result := 0; |
|||
FOR s pos FROM LWB s TO UPB s DO |
|||
result *:= 10 +:= ( ABS s[ s pos ] - ABS "0" ) |
|||
OD; |
|||
result |
|||
END # TOINT # ; |
|||
# returns the first digit of n # |
|||
OP FIRSTDIGIT = ( INT n )INT: |
|||
BEGIN |
|||
INT result := ABS n; |
|||
WHILE result > 9 DO result OVERAB 10 OD; |
|||
result |
|||
END # FIRSTDIGIT # ; |
|||
# returns a string representaton of n # |
|||
OP TOSTRING = ( INT n )STRING: whole( n, 0 ); |
|||
# returns an array containing the values of a sorted such that concatenating the values would result in the largest value # |
|||
OP CONCATSORT = ( []INT a )[]INT: |
|||
IF LWB a >= UPB a THEN |
|||
# 0 or 1 element(s) # |
|||
a |
|||
ELSE |
|||
# 2 or more elements # |
|||
[ 1 : ( UPB a - LWB a ) + 1 ]INT result := a[ AT 1 ]; |
|||
# sort the numbers into reverse first digit order # |
|||
FOR o pos FROM UPB result - 1 BY -1 TO 1 |
|||
WHILE BOOL swapped := FALSE; |
|||
FOR i pos TO o pos DO |
|||
IF FIRSTDIGIT result[ i pos ] < FIRSTDIGIT result[ i pos + 1 ] THEN |
|||
INT t = result[ i pos + 1 ]; |
|||
result[ i pos + 1 ] := result[ i pos ]; |
|||
result[ i pos ] := t; |
|||
swapped := TRUE |
|||
FI |
|||
OD; |
|||
swapped |
|||
DO SKIP OD; |
|||
# now re-order adjacent numbers so they have the highest concatenated value # |
|||
WHILE BOOL swapped := FALSE; |
|||
FOR i pos TO UPB result - 1 DO |
|||
STRING l := TOSTRING result[ i pos ]; |
|||
STRING r := TOSTRING result[ i pos + 1 ]; |
|||
IF TOINT ( l + r ) < TOINT ( r + l ) THEN |
|||
INT t = result[ i pos + 1 ]; |
|||
result[ i pos + 1 ] := result[ i pos ]; |
|||
result[ i pos ] := t; |
|||
swapped := TRUE |
|||
FI |
|||
OD; |
|||
swapped |
|||
DO SKIP OD; |
|||
result |
|||
FI # CONCATSORT # ; |
|||
# prints the array a # |
|||
OP PRINT = ( []INT a )VOID: |
|||
FOR a pos FROM LWB a TO UPB a DO |
|||
print( ( TOSTRING a[ a pos ] ) ) |
|||
OD # PRINT # ; |
|||
# task test cases # |
|||
PRINT CONCATSORT []INT( 1, 34, 3, 98, 9, 76, 45, 4 ); |
|||
print( ( newline ) ); |
|||
PRINT CONCATSORT []INT( 54, 546, 548, 60 ); |
|||
print( ( newline ) ) |
|||
END</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
998764543431 |
|||
6054854654 |
|||
</pre> |
|||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="rebol">largestConcInt: function [arr]-> |
|||
max map permutate arr 's [ |
|||
to :integer join map s => [to :string] |
|||
] |
|||
loop [[1 34 3 98 9 76 45 4] [54 546 548 60]] 'a -> |
|||
print largestConcInt a</syntaxhighlight> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">LargestConcatenatedInts(var){ |
||
StringReplace, var, A_LoopField,%A_Space%,, all |
StringReplace, var, A_LoopField,%A_Space%,, all |
||
Sort, var, D`, fConcSort |
Sort, var, D`, fConcSort |
||
Line 71: | Line 204: | ||
m := a . b , n := b . a |
m := a . b , n := b . a |
||
return m < n ? 1 : m > n ? -1 : 0 |
return m < n ? 1 : m > n ? -1 : 0 |
||
}</ |
}</syntaxhighlight> |
||
Examples:< |
Examples:<syntaxhighlight lang="autohotkey">d = |
||
( |
( |
||
1, 34, 3, 98, 9, 76, 45, 4 |
1, 34, 3, 98, 9, 76, 45, 4 |
||
Line 79: | Line 212: | ||
) |
) |
||
loop, parse, d, `n |
loop, parse, d, `n |
||
MsgBox % LargestConcatenatedInts(A_LoopField)</ |
MsgBox % LargestConcatenatedInts(A_LoopField)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
Line 87: | Line 220: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
{{works with|gawk|4.0}} |
{{works with|gawk|4.0}} |
||
< |
<syntaxhighlight lang="awk"> |
||
function cmp(i1, v1, i2, v2, u1, u2) { |
function cmp(i1, v1, i2, v2, u1, u2) { |
||
u1 = v1""v2; |
u1 = v1""v2; |
||
Line 107: | Line 240: | ||
print largest_int_from_concatenated_ints(X) |
print largest_int_from_concatenated_ints(X) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
Line 113: | Line 246: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic"> DIM Nums%(10) |
||
Nums%()=1,34,3,98,9,76,45,4 |
Nums%()=1,34,3,98,9,76,45,4 |
||
PRINT FNlargestint(8) |
PRINT FNlargestint(8) |
||
Line 133: | Line 266: | ||
l$+=STR$Nums%(i%) |
l$+=STR$Nums%(i%) |
||
NEXT |
NEXT |
||
=l$</ |
=l$</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
Line 139: | Line 272: | ||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<syntaxhighlight lang="bracmat">( ( maxnum |
||
= A Z F C |
= A Z F C |
||
. !arg:# |
. !arg:# |
||
Line 156: | Line 289: | ||
& out$(str$(maxnum$(1 34 3 98 9 76 45 4))) |
& out$(str$(maxnum$(1 34 3 98 9 76 45 4))) |
||
& out$(str$(maxnum$(54 546 548 60))) |
& out$(str$(maxnum$(54 546 548 60))) |
||
);</ |
);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
Line 162: | Line 295: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 192: | Line 325: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
6054854654</pre> |
6054854654</pre> |
||
=={{header|C++}}== |
|||
<lang cpp>#include <iostream> |
|||
#include <sstream> |
|||
#include <algorithm> |
|||
#include <vector> |
|||
#include <string> |
|||
std::string findLargestConcat ( std::vector< int > & mynumbers ) { |
|||
std::vector<std::string> concatnumbers ; |
|||
std::sort ( mynumbers.begin( ) , mynumbers.end( ) ) ; |
|||
do { |
|||
std::ostringstream numberstream ; |
|||
for ( int i : mynumbers ) |
|||
numberstream << i ; |
|||
concatnumbers.push_back( numberstream.str( ) ) ; |
|||
} while ( std::next_permutation( mynumbers.begin( ) , |
|||
mynumbers.end( ) )) ; |
|||
return *( std::max_element( concatnumbers.begin( ) , |
|||
concatnumbers.end( ) ) ) ; |
|||
} |
|||
int main( ) { |
|||
std::vector<int> mynumbers = { 98, 76 , 45 , 34, 9 , 4 , 3 , 1 } ; |
|||
std::vector<int> othernumbers = { 54 , 546 , 548 , 60 } ; |
|||
std::cout << "The largest concatenated int is " << |
|||
findLargestConcat( mynumbers ) << " !\n" ; |
|||
std::cout << "And here it is " << findLargestConcat( othernumbers ) |
|||
<< " !\n" ; |
|||
return 0 ; |
|||
}</lang> |
|||
{{out}} |
|||
<pre>The largest concatenated int is 998764543431 ! |
|||
And here it is 6054854654 !</pre> |
|||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 268: | Line 366: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>The largest possible integer from set 1 is: 998764543431 |
<pre>The largest possible integer from set 1 is: 998764543431 |
||
The largest possible integer from set 2 is: 6054854654</pre> |
The largest possible integer from set 2 is: 6054854654</pre> |
||
=={{header|C++}}== |
|||
<syntaxhighlight lang="cpp">#include <iostream> |
|||
#include <sstream> |
|||
#include <algorithm> |
|||
#include <vector> |
|||
#include <string> |
|||
std::string findLargestConcat ( std::vector< int > & mynumbers ) { |
|||
std::vector<std::string> concatnumbers ; |
|||
std::sort ( mynumbers.begin( ) , mynumbers.end( ) ) ; |
|||
do { |
|||
std::ostringstream numberstream ; |
|||
for ( int i : mynumbers ) |
|||
numberstream << i ; |
|||
concatnumbers.push_back( numberstream.str( ) ) ; |
|||
} while ( std::next_permutation( mynumbers.begin( ) , |
|||
mynumbers.end( ) )) ; |
|||
return *( std::max_element( concatnumbers.begin( ) , |
|||
concatnumbers.end( ) ) ) ; |
|||
} |
|||
int main( ) { |
|||
std::vector<int> mynumbers = { 98, 76 , 45 , 34, 9 , 4 , 3 , 1 } ; |
|||
std::vector<int> othernumbers = { 54 , 546 , 548 , 60 } ; |
|||
std::cout << "The largest concatenated int is " << |
|||
findLargestConcat( mynumbers ) << " !\n" ; |
|||
std::cout << "And here it is " << findLargestConcat( othernumbers ) |
|||
<< " !\n" ; |
|||
return 0 ; |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre>The largest concatenated int is 998764543431 ! |
|||
And here it is 6054854654 !</pre> |
|||
=={{header|Ceylon}}== |
|||
{{trans|Kotlin}} |
|||
{{works with|Ceylon|1.3.3}} |
|||
<syntaxhighlight lang="ceylon">shared void run() { |
|||
function comparator(Integer x, Integer y) { |
|||
assert (is Integer xy = Integer.parse(x.string + y.string), |
|||
is Integer yx = Integer.parse(y.string + x.string)); |
|||
return yx <=> xy; |
|||
} |
|||
function biggestConcatenation({Integer*} ints) => "".join(ints.sort(comparator)); |
|||
value test1 = {1, 34, 3, 98, 9, 76, 45, 4}; |
|||
print(biggestConcatenation(test1)); |
|||
value test2 = {54, 546, 548, 60}; |
|||
print(biggestConcatenation(test2)); |
|||
}</syntaxhighlight> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(defn maxcat [coll] |
||
(read-string |
(read-string |
||
(apply str |
(apply str |
||
Line 283: | Line 436: | ||
coll)))) |
coll)))) |
||
(prn (map maxcat [[1 34 3 98 9 76 45 4] [54 546 548 60]]))</ |
(prn (map maxcat [[1 34 3 98 9 76 45 4] [54 546 548 60]]))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
|||
<pre>(998764543431 6054854654)</pre> |
<pre>(998764543431 6054854654)</pre> |
||
=={{header|Common Lisp}}== |
|||
=== Sort by two-by-two comparison of largest concatenated result === |
|||
<syntaxhighlight lang="lisp"> |
|||
(defun int-concat (ints) |
|||
(read-from-string (format nil "~{~a~}" ints))) |
|||
(defun by-biggest-result (first second) |
|||
(> (int-concat (list first second)) (int-concat (list second first)))) |
|||
(defun make-largest-int (ints) |
|||
(int-concat (sort ints #'by-biggest-result))) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
> (make-largest-int '(1 34 3 98 9 76 45 4)) |
|||
998764543431 |
|||
> (make-largest-int '(54 546 548 60)) |
|||
6054854654 |
|||
</pre> |
|||
=== Variation around the sort with padded most significant digit === |
|||
<syntaxhighlight lang="lisp"> |
|||
;; Sort criteria is by most significant digit with least digits used as a tie |
|||
;; breaker |
|||
(defun largest-msd-with-less-digits (x y) |
|||
(flet ((first-digit (x) |
|||
(digit-char-p (aref x 0)))) |
|||
(cond ((> (first-digit x) |
|||
(first-digit y)) |
|||
t) |
|||
((> (first-digit y) |
|||
(first-digit x)) |
|||
nil) |
|||
((and (= (first-digit x) |
|||
(first-digit y)) |
|||
(> (length x) |
|||
(length y))) |
|||
nil) |
|||
(t t)))) |
|||
(loop |
|||
:for input :in '((54 546 548 60) (1 34 3 98 9 76 45 4)) |
|||
:do (format t "~{~A~}~%" |
|||
(sort (mapcar #'write-to-string input) |
|||
#'largest-msd-with-less-digits))) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
6054548546 |
|||
998764453341 |
|||
</pre> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
The three algorithms. Uses the second module from the Permutations Task. |
The three algorithms. Uses the second module from the Permutations Task. |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.conv, std.array, permutations2; |
||
auto maxCat1(in int[] arr) pure @safe { |
auto maxCat1(in int[] arr) pure @safe { |
||
Line 311: | Line 523: | ||
const lists = [[1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60]]; |
const lists = [[1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60]]; |
||
[&maxCat1, &maxCat2, &maxCat3].map!(cat => lists.map!cat).writeln; |
[&maxCat1, &maxCat2, &maxCat3].map!(cat => lists.map!cat).writeln; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[["998764543431", "6054854654"], ["998764543431", "6054854654"], ["998764543431", "6054854654"]]</pre> |
<pre>[["998764543431", "6054854654"], ["998764543431", "6054854654"], ["998764543431", "6054854654"]]</pre> |
||
=={{header|Delphi}}== |
|||
See [https://www.rosettacode.org/wiki/Largest_int_from_concatenated_ints#Pascal Pascal]. |
|||
=={{header|EasyLang}}== |
|||
<syntaxhighlight> |
|||
func con a b . |
|||
t = 10 |
|||
while b >= t |
|||
t *= 10 |
|||
. |
|||
return a * t + b |
|||
. |
|||
func$ max a[] . |
|||
n = len a[] |
|||
for i to n - 1 |
|||
for j = i + 1 to n |
|||
if con a[i] a[j] < con a[j] a[i] |
|||
swap a[i] a[j] |
|||
. |
|||
. |
|||
. |
|||
for v in a[] |
|||
r$ &= v |
|||
. |
|||
return r$ |
|||
. |
|||
print max [ 1 34 3 98 9 76 45 4 ] |
|||
print max [ 54 546 548 60 ] |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
998764543431 |
|||
6054854654 |
|||
</pre> |
|||
=={{header|Elixir}}== |
|||
<syntaxhighlight lang="elixir">defmodule RC do |
|||
def largest_int(list) do |
|||
sorted = Enum.sort(list, fn x,y -> "#{x}#{y}" >= "#{y}#{x}" end) |
|||
Enum.join(sorted) |
|||
end |
|||
end |
|||
IO.inspect RC.largest_int [1, 34, 3, 98, 9, 76, 45, 4] |
|||
IO.inspect RC.largest_int [54, 546, 548, 60]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
"998764543431" |
|||
"6054854654" |
|||
</pre> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
-module( largest_int_from_concatenated ). |
-module( largest_int_from_concatenated ). |
||
Line 328: | Line 592: | ||
task() -> |
task() -> |
||
[io:fwrite("Largest ~p from ~p~n", [ints(X), X]) || X <- [[1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60]]]. |
[io:fwrite("Largest ~p from ~p~n", [ints(X), X]) || X <- [[1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60]]]. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 334: | Line 598: | ||
Largest 998764543431 from [1,34,3,98,9,76,45,4] |
Largest 998764543431 from [1,34,3,98,9,76,45,4] |
||
Largest 6054854654 from [54,546,548,60] |
Largest 6054854654 from [54,546,548,60] |
||
</pre> |
|||
=={{header|F_Sharp|F#}}== |
|||
<syntaxhighlight lang="fsharp"> |
|||
// Form largest integer which is a permutation from a list of integers. Nigel Galloway: March 21st., 2018 |
|||
let fN g = List.map (string) g |> List.sortWith(fun n g->if n+g<g+n then 1 else -1) |> System.String.Concat |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
fN [1; 34; 3; 98; 9; 76; 45; 4] -> "998764543431" |
|||
fN [54; 546; 548; 60] -> "6054854654" |
|||
</pre> |
|||
=={{header|Factor}}== |
|||
Using algorithm 3: |
|||
<syntaxhighlight lang="factor">USING: assocs io kernel math qw sequences sorting ; |
|||
IN: rosetta-code.largest-int |
|||
: pad ( target seq -- padded ) |
|||
2dup length / swap <repetition> concat swap head ; |
|||
: largest-int ( seq -- ) |
|||
dup dup [ length ] map supremum ! find longest length so we know how much to pad |
|||
[ swap pad ] curry map ! pad the integers |
|||
<enum> sort-values ! sort the padded integers |
|||
keys ! find the original indices of the sorted integers |
|||
swap nths ! order non-padded integers according to their sorted order |
|||
reverse concat print ; |
|||
qw{ 1 34 3 98 9 76 45 4 } qw{ 54 546 548 60 } [ largest-int ] bi@</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
998764543431 |
|||
6054854654 |
|||
</pre> |
|||
Or alternatively, a translation of F#. |
|||
{{trans|F#}} |
|||
<syntaxhighlight lang="factor">USING: kernel math.order qw sequences sorting ; |
|||
: fn ( seq -- str ) |
|||
[ 2dup swap [ append ] 2bi@ after? +lt+ +gt+ ? ] sort concat ;</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
qw{ 1 34 3 98 9 76 45 4 } qw{ 54 546 548 60 } [ fn ] bi@ |
|||
--- Data stack: |
|||
"998764543431" |
|||
"6054854654"</pre> |
|||
=={{header|Fortran}}== |
|||
There is often a potential ambiguity when reading numbers. While three definitely names the Platonic number notion, 3 might instead be regarded as being a text that happens to have the glyph of a number but is not a number. This sort of discussion arises when a spreadsheet has read in a text file and behold! numbers are on the display and they look just like what is displayed when numbers are being shown, but, they are ''not'' numbers, they are only drawn that way. Within the spreadsheet they are parts of some text, and the notion that takes over is one of a "blunt, heavy object", not alas close to hand. |
|||
So, the plan is to regard the numbers as being text sequences aligned to the left, containing only digit characters of course - except for the fact that CHARACTER variables often end up having trailing spaces. F2003 formalised a scheme whereby such variables can be "cut-to-fit" as execution proceeds but with earlier Fortrans the standard method is to pay attention to the number of characters in use. F90 introduced a function LEN_TRIM(text) to return the index of the last non-blank character in a text so the only problem now is to decide on how long might the largest number be (and by representing numbers as text strings, there is no difficulty with the limits of INTEGER*2 or INTEGER*4 ''etc.''), and what will be the maximum number of numbers. By devising a subroutine to do the work, these issues can be handled by the caller that is providing the data. The subroutine however intends to sort the collection of texts. This could be done by damaging its parameter which might be regarded as impolite or even unwanted so instead the sort is effected via an array XLAT and juggling its values. This has the advantage that the possibly large elements of the text array are not being moved about, but means that the subroutine must be able to have an XLAT array that is "large enough". F90 standardised the ability for a routine to declare such an array at run-time; previously, arrays within a subroutine (or indeed anywhere) had to have a size fixed at compilation time. In the past this might have been handled by the caller supplying such an array as an additional parameter. |
|||
Passing arrays as parameters can be tricky, especially for multi-dimensional arrays. This uses the old style whereby the size is left unstated via the * in <code>TEXT(*)</code>, though one could use <code>TEXT(N)</code> instead - but at the risk that the actual value of N is wrong and array index checking might be confused thereby. Still earlier one would simply place some integer value there, any valid integer, as in <code>TEXT(666)</code>, and not worry about bound checking at all because old-style compilers did not produce checking code even if it was wanted. F90 standardised the MODULE protocol, within which the size is specified as <code>TEXT(:)</code> whereby secret additional parameters are supplied that contain the actual bound information and bound checking will be correct, possibly not so if the <code>TEXT(N)</code> form is used instead and N is wrong. This extra overhead in every use is possibly better than undetected errors in some uses... |
|||
The sorting of the text array was to be by the notorious BubbleSort, taking advantage of the fact that each pass delivers the maximum value of the unsorted portion to its final position: the output could thereby be produced as the sort worked. Rather than mess about with early termination (no element being swapped) or attention to the bounds within which swapping took place, attention concentrated upon the comparison. Because of the left-alignment of the texts, a simple comparison seemed sufficient until I thought of unequal text lengths and then the following example. Suppose there are two numbers, 5, and one of 54, 55, or 56 as the other. Via normal comparisons, the 5 would always be first (because short texts are considered expanded with trailing spaces when compared against longer texts, and a space precedes every digit) however the biggest ordering is 5 54 for the first case but 56 5 for the last. This possibility is not exemplified in the specified trial sets. So, a more complex comparison is required. One could of course write a suitable function and consider the issue there but instead the comparison forms the compound text in the same manner as the result will be, in the two ways AB and BA, and looks to see which yields the bigger sequence. This need only be done for unequal length text pairs. |
|||
The source is F77 style, except for the declaration of XLAT(N), the use of <N> in the FORMAT statements instead of some large constant or similar, and the ability to declare an array via constants as in <code>(/"5","54"/)</code> rather than mess about declaring arrays and initialising them separately. The <code>I0</code> format code to convert a number (an actual number) into a digit string aligned leftwards in a CHARACTER variable of sufficient size is also a F90 introduction, though the B6700 compiler allowed a code <code>J</code> instead. This last is to demonstrate usage of actual numbers for those unpersuaded by the argument for ambiguity that allows for texts. If the <code>I0</code> format code is unavailable then <code>I9</code> (or some suitable size) could be used, followed by <code>text = ADJUSTL(text)</code>, except that this became an intrinsic function only in F90, so perhaps you will have to write a simple alignment routine. <syntaxhighlight lang="fortran"> SUBROUTINE SWAP(A,B) !Why can't the compiler supply these! |
|||
INTEGER A,B,T |
|||
T = B |
|||
B = A |
|||
A = T |
|||
END |
|||
SUBROUTINE BIGUP(TEXT,N) !Outputs the numbers in TEXT to give the biggest number. |
|||
CHARACTER*(*) TEXT(*) !The numbers as text, aligned left. |
|||
INTEGER N !The number of them. |
|||
INTEGER XLAT(N),L(N) !An index and a set of lengths. |
|||
INTEGER I,J,M !Assorted steppers. |
|||
INTEGER TI,TJ !Fingers to a text. |
|||
INTEGER LI,LJ !Lengths of the fingered texts. |
|||
INTEGER MSG !I/O unit number. |
|||
COMMON /IODEV/ MSG !Old style. |
|||
DO I = 1,N !Step through my supply of texts. |
|||
XLAT(I) = I !Preparing a finger to them. |
|||
L(I) = LEN_TRIM(TEXT(I)) !And noting their last non-blank. |
|||
END DO !On to the next. |
|||
WRITE (MSG,1) "Supplied",(TEXT(I)(1:L(I)), I = 1,N) !Show the grist. |
|||
1 FORMAT (A12,":",<N>(A,",")) !Instead of <N>, 666 might suffice. |
|||
Crude bubblesort. No attempt at noting the bounds of swaps made. |
|||
DO M = N,1,-1 !Just for fun, go backwards. |
|||
DO I = 2,M !Start a scan. |
|||
J = I - 1 !Comparing element I to element I - 1. |
|||
TI = XLAT(I) !Thus finger the I'th text in XLAT order. |
|||
TJ = XLAT(J) !And its supposed predecessor. |
|||
LI = L(TI) !The length of the fingered text. |
|||
LJ = L(TJ) !All this to save on typing below. |
|||
IF (LI .EQ. LJ) THEN !If the texts are equal lengths, |
|||
IF (TEXT(TI).LT.TEXT(TJ)) CALL SWAP(XLAT(I),XLAT(J)) !A simple comparison. |
|||
ELSE !But if not, construct the actual candidate texts for comparison. |
|||
IF (TEXT(TI)(1:LI)//TEXT(TJ)(1:LJ) !These two will be the same length. |
|||
1 .LT.TEXT(TJ)(1:LJ)//TEXT(TI)(1:LI)) !Just as above. |
|||
2 CALL SWAP(XLAT(I),XLAT(J)) !J shall now follow I. |
|||
END IF !So much for that comparison. |
|||
END DO !On to the next. |
|||
END DO !The original plan was to reveal element XLAT(M) as found. |
|||
WRITE (MSG,2) "Biggest",(TEXT(XLAT(I))(1:L(XLAT(I))),I = N,1,-1) !But, all at once is good too. |
|||
2 FORMAT (A12,":",<N>(A," ")) !The space maintains identity. |
|||
END !That was fun. |
|||
PROGRAM POKE |
|||
CHARACTER*4 T1(10) !Prepare some example arrays. |
|||
CHARACTER*4 T2(4) !To hold the specified examples. |
|||
INTEGER MSG |
|||
COMMON /IODEV/ MSG |
|||
DATA T1(1:8)/"1","34","3","98","9","76","45","4"/ |
|||
DATA T2/"54","546","548","60"/ |
|||
MSG = 6 !Standard output. |
|||
WRITE (MSG,1) |
|||
1 FORMAT ("Takes a list of integers and concatenates them so as ", |
|||
1 "to produce the biggest possible number.",/, |
|||
2 "The result is shown with spaces between the parts ", |
|||
3 "to show provenance. Ignore them otherwise."/) |
|||
CALL BIGUP(T1,8) |
|||
WRITE (MSG,*) |
|||
CALL BIGUP(T2,4) |
|||
WRITE (MSG,*) "These are supplied in lexicographical order..." |
|||
CALL BIGUP((/"5","54"/),2) |
|||
WRITE (MSG,*) "But this is not necessarily the biggest order." |
|||
CALL BIGUP((/"5","56"/),2) |
|||
WRITE (MSG,*) "And for those who count..." |
|||
DO I = 1,10 |
|||
WRITE (T1(I),"(I0)") I !This format code produces only the necessary text. |
|||
END DO !Thus, the numbers are aligned left in the text field. |
|||
CALL BIGUP(T1,10) |
|||
END </syntaxhighlight> |
|||
Output: the Fortran compiler ignores spaces when reading fortran source, so, hard-core fortranners should have no difficulty doing likewise for the output... |
|||
<pre> |
|||
Takes a list of integers and concatenates them so as to produce the biggest possible number. |
|||
The result is shown with spaces between the parts to show provenance. Ignore them otherwise. |
|||
Supplied:1,34,3,98,9,76,45,4, |
|||
Biggest:9 98 76 45 4 34 3 1 |
|||
Supplied:54,546,548,60, |
|||
Biggest:60 548 546 54 |
|||
These are supplied in lexicographical order... |
|||
Supplied:5,54, |
|||
Biggest:5 54 |
|||
But this is not necessarily the biggest order. |
|||
Supplied:5,56, |
|||
Biggest:56 5 |
|||
And for those who count... |
|||
Supplied:1,2,3,4,5,6,7,8,9,10, |
|||
Biggest:9 8 7 6 5 4 3 2 1 10 |
|||
</pre> |
|||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="freebasic">#define MAXDIGITS 8 |
|||
function catint( a as string, b as string ) as uinteger |
|||
return valint(a+b) |
|||
end function |
|||
function grt( a as string, b as string ) as boolean |
|||
return catint(a, b)>catint(b, a) |
|||
end function |
|||
sub shellsort( a() as string ) |
|||
'quick and dirty shellsort, not the focus of this exercise |
|||
dim as uinteger gap = ubound(a), i, j, n=ubound(a) |
|||
dim as string temp |
|||
do |
|||
gap = int(gap / 2.2) |
|||
for i=gap to n |
|||
temp = a(i) |
|||
j=i |
|||
while j>=gap andalso grt( a(j-gap), temp ) |
|||
a(j) = a(j - gap) |
|||
j -= gap |
|||
wend |
|||
a(j) = temp |
|||
next i |
|||
loop until gap = 1 |
|||
end sub |
|||
sub sort_and_print( a() as string ) |
|||
dim as uinteger i |
|||
dim as string outstring = "" |
|||
shellsort(a()) |
|||
for i=0 to ubound(a) |
|||
outstring = a(i)+outstring |
|||
next i |
|||
print outstring |
|||
end sub |
|||
dim as string set1(8) = {"1", "34", "3", "98", "9", "76", "45", "4"} |
|||
dim as string set2(4) = {"54", "546", "548", "60"} |
|||
sort_and_print(set1()) |
|||
sort_and_print(set2())</syntaxhighlight> |
|||
{{out}} |
|||
<pre>998764543431 |
|||
6054854654 |
|||
</pre> |
|||
=={{header|Frink}}== |
|||
<syntaxhighlight lang="frink">a = [[1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60]] |
|||
f = {|p| parseInt[join["",p]] } |
|||
for s = a |
|||
println[max[map[f, s.lexicographicPermute[]]]]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
998764543431 |
|||
6054854654 |
|||
</pre> |
|||
=={{header|Gambas}}== |
|||
'''[https://gambas-playground.proko.eu/?gist=4169e7641f29ff0ae1dd202b459e60ce Click this link to run this code]''' |
|||
<syntaxhighlight lang="gambas">'Largest int from concatenated ints |
|||
Public Sub Main() |
|||
Dim iList1 As Integer[] = [1, 34, 3, 98, 9, 76, 45, 4] 'Integer list 1 |
|||
Dim iList2 As Integer[] = [54, 546, 548, 60] 'Integer list 2 |
|||
Calc(iList1) 'Send List 1 to Calc routine |
|||
Calc(iList2) 'Send List 2 to Calc routine |
|||
End |
|||
'_________________________________________________________________________________________ |
|||
Public Sub Calc(iList As Integer[]) |
|||
Dim siCount1, siCount2, siCounter As Short 'Counters |
|||
Dim sList As New String[] 'To hold converted integers |
|||
Dim bTrigger As Boolean 'To trigger a found match |
|||
For Each siCount1 In iList 'For each integer in the list.. |
|||
sList.Add(Str(siCount1)) 'Convert to a string and add to sList |
|||
If Len(Str(siCount1)) > siCounter Then 'If the length of the string is greater than siCounter then.. |
|||
siCounter = Len(Str(siCount1)) 'siCounter = length of the string |
|||
End If |
|||
Next |
|||
For siCount1 = 0 To sList.Max 'For each item in sList |
|||
If Len(sList[siCount1]) < siCounter Then 'If the length of the string is less that siCounter then.. |
|||
sList[siCount1] &= Right(sList[siCount1], 1) 'Add the same digit to the string e.g. in list 1 "9" becomes "99", list 2 "54" becomes "544" |
|||
End If |
|||
Next |
|||
sList.Sort(gb.Descent) 'Sort the list in decending order |
|||
For siCount1 = 0 To sList.Max 'For each item in sList |
|||
bTrigger = False 'Set bTrigger to False |
|||
For siCount2 = 0 To iList.Max 'Loop through each item in iList |
|||
If Val(sList[siCount1]) = iList[siCount2] Then 'If the value of each is the same e.g. "98" = 98 then |
|||
bTrigger = True 'Set bTrigger to True |
|||
Continue 'Exit the loop |
|||
Endif |
|||
Next |
|||
If Not bTrigger Then 'If there was no match e.g. there is no "99" then.. |
|||
sList[siCount1] = Left(sList[siCount1], siCounter - 1) 'Strip out the end digit e.g. "99" becomes 9 again |
|||
End If |
|||
Next |
|||
Print Val(sList.Join("")) 'Join all items in sList together and print |
|||
End</syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
998764543431 |
|||
6054854654 |
|||
</pre> |
</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">// Variation of method 3. Repeat digits to at least the size of the longest, |
||
// then sort as strings. |
// then sort as strings. |
||
package main |
package main |
||
Line 389: | Line 920: | ||
fmt.Println(li(1, 34, 3, 98, 9, 76, 45, 4)) |
fmt.Println(li(1, 34, 3, 98, 9, 76, 45, 4)) |
||
fmt.Println(li(54, 546, 548, 60)) |
fmt.Println(li(54, 546, 548, 60)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 397: | Line 928: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">def largestInt = { c -> c.sort { v2, v1 -> "$v1$v2" <=> "$v2$v1" }.join('') as BigInteger }</syntaxhighlight> |
||
Testing: |
Testing: |
||
< |
<syntaxhighlight lang="groovy">assert largestInt([1, 34, 3, 98, 9, 76, 45, 4]) == 998764543431 |
||
assert largestInt([54, 546, 548, 60]) == 6054854654</ |
assert largestInt([54, 546, 548, 60]) == 6054854654</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
===Compare repeated string method=== |
===Compare repeated string method=== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (sortBy) |
||
import Data.Ord (comparing) |
import Data.Ord (comparing) |
||
Line 413: | Line 944: | ||
in sortBy (flip $ comparing pad) xs |
in sortBy (flip $ comparing pad) xs |
||
maxcat = read . concat . sorted . map show</ |
maxcat = read . concat . sorted . map show</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 419: | Line 950: | ||
Since repeating numerical string "1234" is the same as taking all the digits of 1234/9999 after the decimal point, the following does essentially the same as above: |
Since repeating numerical string "1234" is the same as taking all the digits of 1234/9999 after the decimal point, the following does essentially the same as above: |
||
< |
<syntaxhighlight lang="haskell">import Data.List (sortBy) |
||
import Data.Ord (comparing) |
import Data.Ord (comparing) |
||
import Data.Ratio ((%)) |
import Data.Ratio ((%)) |
||
Line 429: | Line 960: | ||
map (\a->(a, head $ dropWhile (<a) nines)) |
map (\a->(a, head $ dropWhile (<a) nines)) |
||
main = mapM_ (print.maxcat) [[1,34,3,98,9,76,45,4], [54,546,548,60]]</ |
main = mapM_ (print.maxcat) [[1,34,3,98,9,76,45,4], [54,546,548,60]]</syntaxhighlight> |
||
===Sort on comparison of concatenated ints method=== |
===Sort on comparison of concatenated ints method=== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (sortBy) |
||
main = print (map maxcat [[1,34,3,98,9,76,45,4], [54,546,548,60]] :: [Integer]) |
main = print (map maxcat [[1,34,3,98,9,76,45,4], [54,546,548,60]] :: [Integer]) |
||
where sorted = sortBy (\a b -> compare (b++a) (a++b)) |
where sorted = sortBy (\a b -> compare (b++a) (a++b)) |
||
maxcat = read . concat . sorted . map show</ |
maxcat = read . concat . sorted . map show</syntaxhighlight> |
||
;Output as above. |
;Output as above. |
||
===Try all permutations method=== |
===Try all permutations method=== |
||
<lang |
<syntaxhighlight lang="haskell">import Data.List (permutations) |
||
main :: IO () |
|||
main = print (map maxcat [[1,34,3,98,9,76,45,4], [54,546,548,60]] :: [Integer]) |
|||
main = |
|||
where maxcat = read . last . sort . map (concat . map show) . permutations</lang> |
|||
print |
|||
(maxcat <$> [[1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60]] :: [Integer]) |
|||
where |
|||
maxcat = read . maximum . fmap (concatMap show) . permutations</syntaxhighlight> |
|||
;Output as above. |
;Output as above. |
||
Line 453: | Line 988: | ||
lifting. |
lifting. |
||
< |
<syntaxhighlight lang="unicon">import Collections # For the Heap (dense priority queue) class |
||
procedure main(a) |
procedure main(a) |
||
Line 466: | Line 1,001: | ||
procedure cmp(a,b) |
procedure cmp(a,b) |
||
return (a||b) > (b||a) |
return (a||b) > (b||a) |
||
end</ |
end</syntaxhighlight> |
||
Sample runs: |
Sample runs: |
||
Line 478: | Line 1,013: | ||
=={{header|J}}== |
=={{header|J}}== |
||
Here we use the "pad the integers to the same size by repeating the digits then sort using these repeated integers as a sort key" approach: |
|||
'''Solution:''' |
'''Solution:''' |
||
< |
<syntaxhighlight lang="j">maxlen=: [: >./ #&> |
||
maxnum=: (0 ". ;)@(\: maxlen $&> ])@(8!:0)</ |
maxnum=: (0 ". ;)@(\: maxlen $&> ])@(8!:0)</syntaxhighlight> |
||
'''Usage:''' |
'''Usage:''' |
||
< |
<syntaxhighlight lang="j"> maxnum&> 1 34 3 98 9 76 45 4 ; 54 546 548 60 |
||
998764543431 6054854654</ |
998764543431 6054854654</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 489: | Line 1,025: | ||
This example sets up a comparator to order the numbers using <code>Collections.sort</code> as described in method #3 (padding and reverse sorting). |
This example sets up a comparator to order the numbers using <code>Collections.sort</code> as described in method #3 (padding and reverse sorting). |
||
It was also necessary to make a join method to meet the output requirements. |
It was also necessary to make a join method to meet the output requirements. |
||
< |
<syntaxhighlight lang="java5">import java.util.*; |
||
public class IntConcat { |
public class IntConcat { |
||
Line 530: | Line 1,066: | ||
System.out.println(join(ints2)); |
System.out.println(join(ints2)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{works with|Java|1.8+}} |
{{works with|Java|1.8+}} |
||
< |
<syntaxhighlight lang="java5">import java.util.Comparator; |
||
import java.util.stream.Collectors; |
import java.util.stream.Collectors; |
||
import java.util.stream.Stream; |
import java.util.stream.Stream; |
||
Line 579: | Line 1,115: | ||
); |
); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
6054854654</pre> |
6054854654</pre> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
<pre><code> |
|||
===ES5=== |
|||
maxCombine = function (list) { |
|||
list.sort( |
|||
<syntaxhighlight lang="javascript"> (function () { |
|||
function(x,y) |
|||
'use strict'; |
|||
a = x.toString(); |
|||
// maxCombine :: [Int] -> Int |
|||
b = y.toString(); |
|||
function maxCombine(xs) { |
|||
num1 = parseInt(a + b); |
|||
return parseInt( |
|||
xs.sort( |
|||
function (x, y) { |
|||
var a = x.toString(), |
|||
b = y.toString(), |
|||
) |
ab = parseInt(a + b), |
||
ba = parseInt(b + a); |
|||
}; |
|||
return ab > ba ? -1 : (ab < ba ? 1 : 0); |
|||
</code></pre> |
|||
} |
|||
) |
|||
.join(''), 10 |
|||
); |
|||
} |
|||
return [ |
|||
[1, 34, 3, 98, 9, 76, 45, 4], |
|||
[54, 546, 548, 60] |
|||
].map(maxCombine); |
|||
})(); |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>[998764543431, 6054854654]</pre> |
|||
===ES6=== |
|||
<syntaxhighlight lang="javascript">var maxCombine = (a) => +(a.sort((x, y) => +("" + y + x) - +("" + x + y)).join('')); |
|||
// test & output |
|||
console.log([ |
|||
[1, 34, 3, 98, 9, 76, 45, 4], |
|||
[54, 546, 548, 60] |
|||
].map(maxCombine));</syntaxhighlight> |
|||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{Works with|jq|1.4}} |
{{Works with|jq|1.4}} |
||
==== Padding ==== |
==== Padding ==== |
||
''For jq versions greater than 1.4, it may be necessary to change "sort_by" to "sort".'' |
''For jq versions greater than 1.4, it may be necessary to change "sort_by" to "sort".'' |
||
< |
<syntaxhighlight lang="jq">def largest_int: |
||
def pad(n): . + (n - length) * .[length-1:]; |
def pad(n): . + (n - length) * .[length-1:]; |
||
Line 618: | Line 1,184: | ||
([1, 34, 3, 98, 9, 76, 45, 4], |
([1, 34, 3, 98, 9, 76, 45, 4], |
||
[54, 546, 548, 60]) | largest_int |
[54, 546, 548, 60]) | largest_int |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
$ /usr/local/bin/jq -n -M -r -f Largest_int_from_concatenated_ints.jq |
$ /usr/local/bin/jq -n -M -r -f Largest_int_from_concatenated_ints.jq |
||
Line 626: | Line 1,192: | ||
====Custom Sort==== |
====Custom Sort==== |
||
The following uses [[Sort_using_a_custom_comparator#jq| quicksort/1]]: |
The following uses [[Sort_using_a_custom_comparator#jq| quicksort/1]]: |
||
< |
<syntaxhighlight lang="jq">def largest_int: |
||
map(tostring) |
map(tostring) |
||
| quicksort( .[0] + .[1] < .[1] + .[0] ) |
| quicksort( .[0] + .[1] < .[1] + .[0] ) |
||
| reverse | join("") ;</ |
| reverse | join("") ;</syntaxhighlight> |
||
=={{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. |
|||
<syntaxhighlight lang="julia">function maxconcat(arr::Vector{<:Integer}) |
|||
b = sort(string.(arr); lt=(x, y) -> x * y < y * x, rev=true) |> join |
|||
return try parse(Int, b) catch parse(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 arr in tests |
|||
println("Max concatenating in $arr:\n -> ", maxconcat(arr)) |
|||
end</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Max concatenating in [1, 34, 3, 98, 9, 76, 45, 4]: |
|||
-> 998764543431 |
|||
Max concatenating in [54, 546, 548, 60]: |
|||
-> 6054854654 |
|||
Max concatenating in [1, 34, 3, 98, 9, 76, 45, 4, 54, 546, 548, 60]: |
|||
-> 9987660548546544543431</pre> |
|||
=={{header|Kotlin}}== |
|||
{{trans|C#}} |
|||
<syntaxhighlight lang="kotlin">import kotlin.Comparator |
|||
fun main(args: Array<String>) { |
|||
val comparator = Comparator<Int> { x, y -> "$x$y".compareTo("$y$x") } |
|||
fun findLargestSequence(array: IntArray): String { |
|||
return array.sortedWith(comparator.reversed()).joinToString("") { it.toString() } |
|||
} |
|||
for (array in listOf( |
|||
intArrayOf(1, 34, 3, 98, 9, 76, 45, 4), |
|||
intArrayOf(54, 546, 548, 60), |
|||
)) { |
|||
println("%s -> %s".format(array.contentToString(), findLargestSequence(array))) |
|||
} |
|||
}</syntaxhighlight> |
|||
{{Out}} |
|||
<pre> |
|||
[1, 34, 3, 98, 9, 76, 45, 4] -> 998764543431 |
|||
[54, 546, 548, 60] -> 6054854654 |
|||
</pre> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="lua">function icsort(numbers) |
||
table.sort(numbers,function(x,y) return (x..y) > (y..x) end) |
table.sort(numbers,function(x,y) return (x..y) > (y..x) end) |
||
return numbers |
return numbers |
||
Line 643: | Line 1,257: | ||
table.concat(numbers,","),table.concat(icsort(numbers)) |
table.concat(numbers,","),table.concat(icsort(numbers)) |
||
)) |
)) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Numbers: {1,34,3,98,9,76,45,4} |
<pre>Numbers: {1,34,3,98,9,76,45,4} |
||
Line 650: | Line 1,264: | ||
Largest integer: 6054854654</pre> |
Largest integer: 6054854654</pre> |
||
=={{header|Mathematica}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">makeLargestInt[list_] := Module[{sortedlist}, |
||
sortedlist = Sort[list, Order[ToString[#1] <> ToString[#2], ToString[#2] <> ToString[#1]] < 0 &]; |
sortedlist = Sort[list, Order[ToString[#1] <> ToString[#2], ToString[#2] <> ToString[#1]] < 0 &]; |
||
Map[ToString, sortedlist] // StringJoin // FromDigits |
Map[ToString, sortedlist] // StringJoin // FromDigits |
||
Line 657: | Line 1,271: | ||
(* testing with two examples *) |
(* testing with two examples *) |
||
makeLargestInt[{1, 34, 3, 98, 9, 76, 45, 4}] |
makeLargestInt[{1, 34, 3, 98, 9, 76, 45, 4}] |
||
makeLargestInt[{54, 546, 548, 60}]</ |
makeLargestInt[{54, 546, 548, 60}]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
6054854654</pre> |
6054854654</pre> |
||
=={{header|Maxima}}== |
|||
<syntaxhighlight lang="maxima"> |
|||
/* Function that decompose a number into a list of its digits using conversions between numbers and strings */ |
|||
decompose_n_s(n):=block( |
|||
string(n), |
|||
charlist(%%), |
|||
map(eval_string,%%))$ |
|||
/* Function that orders the list obtained by decompose_n_ according to ordergreat and then orders the result to reached what is needed to solve the problem */ |
|||
largest_from_list(lst):=( |
|||
sort(map(decompose_n_s,lst),ordergreatp), |
|||
sort(%%,lambda([a,b],if last(a)>last(b) then rest(b,-1)=a else rest(a,-1)=b)), |
|||
map(string,flatten(%%)), |
|||
simplode(%%), |
|||
eval_string(%%)); |
|||
/* Test cases */ |
|||
test1: [1, 34, 3, 98, 9, 76, 45, 4]$ |
|||
test2: [54, 546, 548, 60]$ |
|||
largest_from_list(test1); |
|||
largest_from_list(test2); |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
998764543431 |
|||
6054854654 |
|||
</pre> |
|||
=={{header|min}}== |
|||
{{works with|min|0.19.6}} |
|||
<syntaxhighlight lang="min">(quote cons "" join) :s+ |
|||
('string map (over over swap s+ 's+ dip <) sort "" join int) :fn |
|||
(1 34 3 98 9 76 45 4) fn puts! |
|||
(54 546 548 60) fn puts!</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
998764543431 |
|||
6054854654 |
|||
</pre> |
|||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 712: | Line 1,365: | ||
end il |
end il |
||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 720: | Line 1,373: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import algorithm, sequtils, strutils, sugar |
||
proc maxNum(x: seq[int]): string = |
proc maxNum(x: seq[int]): string = |
||
var c = x.mapIt( |
var c = x.mapIt($it) |
||
c.sort((x, y) => cmp(y&x, x&y)) |
c.sort((x, y) => cmp(y&x, x&y)) |
||
c.join() |
c.join() |
||
echo maxNum(@[1, 34, 3, 98, 9, 76, 45, 4]) |
echo maxNum(@[1, 34, 3, 98, 9, 76, 45, 4]) |
||
echo maxNum(@[54, 546, 548, 60])</ |
echo maxNum(@[54, 546, 548, 60])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
Line 734: | Line 1,388: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let myCompare a b = compare (b ^ a) (a ^ b) |
||
let icsort nums = String.concat "" (List.sort myCompare (List.map string_of_int nums))</ |
let icsort nums = String.concat "" (List.sort myCompare (List.map string_of_int nums))</syntaxhighlight> |
||
;testing |
;testing |
||
Line 748: | Line 1,402: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">: largestInt map(#asString) sortWith(#[ 2dup + -rot swap + > ]) sum asInteger ;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
[ [1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60] ] map(#largestInt) |
[ [1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60] ] map(#largestInt) . |
||
[998764543431, 6054854654] |
[998764543431, 6054854654] |
||
</pre> |
</pre> |
||
=={{header|PARI/GP}}== |
|||
Sorts then joins. Most of the noise comes from converting a vector of integers into a concatenated integer: <code>eval(concat(apply(n->Str(n),v)))</code>. Note that the short form <code>eval(concat(apply(Str,v)))</code> is not valid here because <code>Str</code> is variadic. |
|||
<syntaxhighlight lang="parigp">large(v)=eval(concat(apply(n->Str(n),vecsort(v,(x,y)->eval(Str(y,x,"-",x,y)))))); |
|||
large([1, 34, 3, 98, 9, 76, 45, 4]) |
|||
large([54, 546, 548, 60])</syntaxhighlight> |
|||
{{out}} |
|||
<pre>%1 = 998764543431 |
|||
%2 = 6054854654</pre> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
tested with freepascal.Used a more extreme example 3. |
tested with freepascal.Used a more extreme example 3. |
||
===algorithm 3=== |
===algorithm 3=== |
||
< |
<syntaxhighlight lang="pascal">const |
||
base = 10; |
base = 10; |
||
MaxDigitCnt = 11; |
MaxDigitCnt = 11; |
||
Line 850: | Line 1,514: | ||
var |
var |
||
i,l : integer; |
i,l : integer; |
||
s : |
s : AnsiString; |
||
begin |
begin |
||
{ the easy way |
{ the easy way |
||
Line 897: | Line 1,561: | ||
InsertData(tmpData[i],source3[i]); |
InsertData(tmpData[i],source3[i]); |
||
HighestInt(tmpData); |
HighestInt(tmpData); |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
Line 906: | Line 1,570: | ||
http://rosettacode.org/wiki/Largest_int_from_concatenated_ints#Compare_repeated_string_method |
http://rosettacode.org/wiki/Largest_int_from_concatenated_ints#Compare_repeated_string_method |
||
< |
<syntaxhighlight lang="pascal">const |
||
base = 10; |
base = 10; |
||
MaxDigitCnt = 11; |
MaxDigitCnt = 11; |
||
Line 1,031: | Line 1,695: | ||
InsertData(tmpData[i],source3[i]); |
InsertData(tmpData[i],source3[i]); |
||
HighestInt(tmpData); |
HighestInt(tmpData); |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>9987645434310 |
<pre>9987645434310 |
||
6054854654 |
6054854654 |
||
602121212122210></pre> |
602121212122210></pre> |
||
=={{header|PARI/GP}}== |
|||
Sorts then joins. Most of the noise comes from converting a vector of integers into a concatenated integer: <code>eval(concat(apply(n->Str(n),v)))</code>. Note that the short form <code>eval(concat(apply(Str,v)))</code> is not valid here because <code>Str</code> is variadic. |
|||
<lang parigp>large(v)=eval(concat(apply(n->Str(n),vecsort(v,(x,y)->eval(Str(y,x,"-",x,y)))))); |
|||
large([1, 34, 3, 98, 9, 76, 45, 4]) |
|||
large([54, 546, 548, 60])</lang> |
|||
{{out}} |
|||
<pre>%1 = 998764543431 |
|||
%2 = 6054854654</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">sub maxnum { |
||
join '', sort { "$b$a" cmp "$a$b" } @_ |
join '', sort { "$b$a" cmp "$a$b" } @_ |
||
} |
} |
||
print maxnum(1, 34, 3, 98, 9, 76, 45, 4), "\n"; |
print maxnum(1, 34, 3, 98, 9, 76, 45, 4), "\n"; |
||
print maxnum(54, 546, 548, 60), "\n";</ |
print maxnum(54, 546, 548, 60), "\n";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
6054854654</pre> |
6054854654</pre> |
||
=={{header| |
=={{header|Phix}}== |
||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<lang perl6>sub maxnum(@x) { |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
[~] @x.sort: -> $a, $b { $b ~ $a leg $a ~ $b } |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">catcmp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> |
|||
} |
|||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">compare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">&</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">&</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
say maxnum <1 34 3 98 9 76 45 4>; |
|||
say maxnum <54 546 548 60>;</lang> |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">method2</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">custom_sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">catcmp</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">)),</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span> |
|||
<span style="color: #0000FF;">?</span><span style="color: #000000;">method2</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">34</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">98</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">76</span><span style="color: #0000FF;">,</span><span style="color: #000000;">45</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">})</span> |
|||
<span style="color: #0000FF;">?</span><span style="color: #000000;">method2</span><span style="color: #0000FF;">({</span><span style="color: #000000;">54</span><span style="color: #0000FF;">,</span><span style="color: #000000;">546</span><span style="color: #0000FF;">,</span><span style="color: #000000;">548</span><span style="color: #0000FF;">,</span><span style="color: #000000;">60</span><span style="color: #0000FF;">})</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
"998764543431" |
|||
6054854654</pre> |
|||
"6054854654" |
|||
</pre> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php">function maxnum($nums) { |
||
usort($nums, function ($x, $y) { return strcmp("$y$x", "$x$y"); }); |
usort($nums, function ($x, $y) { return strcmp("$y$x", "$x$y"); }); |
||
return implode('', $nums); |
return implode('', $nums); |
||
Line 1,076: | Line 1,739: | ||
echo maxnum(array(1, 34, 3, 98, 9, 76, 45, 4)), "\n"; |
echo maxnum(array(1, 34, 3, 98, 9, 76, 45, 4)), "\n"; |
||
echo maxnum(array(54, 546, 548, 60)), "\n";</ |
echo maxnum(array(54, 546, 548, 60)), "\n";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
6054854654</pre> |
6054854654</pre> |
||
=={{header|Picat}}== |
|||
On the simpler cases, four methods are tested: 2 using permutations and 2 with different sorting methods. |
|||
===permutation/2=== |
|||
<syntaxhighlight lang="picat">s_perm1(L, Num) => |
|||
permutation(L,P), |
|||
Num = [I.to_string() : I in P].flatten().to_integer().</syntaxhighlight> |
|||
===Using permutations/1=== |
|||
<syntaxhighlight lang="picat">s_perm2(L, Num) => |
|||
Perms = permutations(L), |
|||
Num = max([ [I.to_string() : I in P].flatten().to_integer() : P in Perms]).</syntaxhighlight> |
|||
===Sort on concatenated numbers=== |
|||
<syntaxhighlight 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).</syntaxhighlight> |
|||
===Extend each element to the largest length=== |
|||
<syntaxhighlight 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().</syntaxhighlight> |
|||
===Test=== |
|||
<syntaxhighlight lang="picat">import util. |
|||
go => |
|||
Ls = [[1, 34, 3, 98, 9, 76, 45, 4], |
|||
[54, 546, 548, 60], |
|||
[97, 9, 13, 979], |
|||
[9, 1, 95, 17, 5] |
|||
], |
|||
foreach(L in Ls) |
|||
test(L) |
|||
end, |
|||
nl. |
|||
% Test all implementations |
|||
test(L) => |
|||
println(l=L), |
|||
maxof_inc(s_perm1(L,Num1), Num1), |
|||
println(s_perm1=Num1), |
|||
s_perm2(L,Num2), |
|||
println(s_perm2=Num2), |
|||
s_sort_conc(L,Num3), |
|||
println(s_sort_conc=Num3), |
|||
s_extend(L,Num4), |
|||
println(s_extent=Num4), |
|||
nl.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>l = [1,34,3,98,9,76,45,4] |
|||
s_perm1 = 998764543431 |
|||
s_perm2 = 998764543431 |
|||
s_sort_conc = 998764543431 |
|||
s_extend = 998764543431 |
|||
l = [54,546,548,60] |
|||
s_perm1 = 6054854654 |
|||
s_perm2 = 6054854654 |
|||
s_sort_conc = 6054854654 |
|||
s_extend = 6054854654 |
|||
l = [97,9,13,979] |
|||
s_perm1 = 99799713 |
|||
s_perm2 = 99799713 |
|||
s_sort_conc = 99799713 |
|||
s_extend = 99799713 |
|||
l = [9,1,95,17,5] |
|||
s_perm1 = 9955171 |
|||
s_perm2 = 9955171 |
|||
s_sort_conc = 9955171 |
|||
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. |
|||
<syntaxhighlight lang="picat">go2 => |
|||
garbage_collect(100_000_000), |
|||
_ = random2(), |
|||
N = 2000, |
|||
println(nums=N), |
|||
L = [random(1,1000) : _ in 1..N], |
|||
S = join([I.to_string : I in L],''), |
|||
println(str_len=S.len), |
|||
nl, |
|||
println("s_sort_conc:"), |
|||
time(s_sort_conc(L,_Num3)), |
|||
println("s_extend:"), |
|||
time(s_extend(L,_Num4)), |
|||
nl.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>nums = 2000 |
|||
str_len = 5805 |
|||
s_sort_conc: |
|||
CPU time 0.54 seconds. |
|||
s_extend: |
|||
CPU time 0.526 seconds.</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Line 1,088: | Line 1,890: | ||
unique lists (as the comparison of identical numbers would not terminate), so a |
unique lists (as the comparison of identical numbers would not terminate), so a |
||
better solution might involve additional checks. |
better solution might involve additional checks. |
||
< |
<syntaxhighlight lang="picolisp">(load "@lib/simul.l") # For 'permute'</syntaxhighlight> |
||
===Algorithm 1=== |
===Algorithm 1=== |
||
< |
<syntaxhighlight lang="picolisp">(for L '((1 34 3 98 9 76 45 4) (54 546 548 60)) |
||
(prinl (maxi format (permute L))) )</ |
(prinl (maxi format (permute L))) )</syntaxhighlight> |
||
===Algorithm 2=== |
===Algorithm 2=== |
||
< |
<syntaxhighlight lang="picolisp">(for L '((1 34 3 98 9 76 45 4) (54 546 548 60)) |
||
(prinl |
(prinl |
||
(sort L |
(sort L |
||
Line 1,099: | Line 1,901: | ||
(> |
(> |
||
(format (pack A B)) |
(format (pack A B)) |
||
(format (pack B A)) ) ) ) ) )</ |
(format (pack B A)) ) ) ) ) )</syntaxhighlight> |
||
===Algorithm 3=== |
===Algorithm 3=== |
||
< |
<syntaxhighlight lang="picolisp">(for L '((1 34 3 98 9 76 45 4) (54 546 548 60)) |
||
(prinl |
(prinl |
||
(flip |
(flip |
||
(by '((N) (apply circ (chop N))) sort L) ) ) )</ |
(by '((N) (apply circ (chop N))) sort L) ) ) )</syntaxhighlight> |
||
{{out}} in all three cases: |
{{out}} in all three cases: |
||
<pre>998764543431 |
<pre>998764543431 |
||
Line 1,110: | Line 1,912: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pli"> |
||
/* Largest catenation of integers 16 October 2013 */ |
/* Largest catenation of integers 16 October 2013 */ |
||
/* Sort using method 2, comparing pairs of adjacent integers. */ |
/* Sort using method 2, comparing pairs of adjacent integers. */ |
||
Line 1,141: | Line 1,943: | ||
end largest_integer; |
end largest_integer; |
||
end Largest; |
end Largest; |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
54 546 548 60 |
54 546 548 60 |
||
Line 1,149: | Line 1,951: | ||
Largest integer= 998764543431 |
Largest integer= 998764543431 |
||
</pre> |
</pre> |
||
=={{header|PowerShell}}== |
|||
{{works with|PowerShell|2}} |
|||
Using algorithm 3 |
|||
<syntaxhighlight lang="powershell">Function Get-LargestConcatenation ( [int[]]$Integers ) |
|||
{ |
|||
# Get the length of the largest integer |
|||
$Length = ( $Integers | Sort -Descending | Select -First 1 ).ToString().Length |
|||
# Convert to an array of strings, |
|||
# sort by each number repeated Length times and truncated to Length, |
|||
# and concatenate (join) |
|||
$Concat = ( [string[]]$Integers | Sort { ( $_ * $Length ).Substring( 0, $Length ) } -Descending ) -join '' |
|||
# Convert to integer (upsizing type if needed) |
|||
try { $Integer = [ int32]$Concat } |
|||
catch { try { $Integer = [ int64]$Concat } |
|||
catch { $Integer = [bigint]$Concat } } |
|||
return $Integer |
|||
}</syntaxhighlight> |
|||
<syntaxhighlight lang="powershell">Get-LargestConcatenation 1, 34, 3, 98, 9, 76, 45, 4 |
|||
Get-LargestConcatenation 54, 546, 548, 60 |
|||
Get-LargestConcatenation 54, 546, 548, 60, 54, 546, 548, 60</syntaxhighlight> |
|||
{{out}} |
|||
<pre>998764543431 |
|||
6054854654 |
|||
60605485485465465454</pre> |
|||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Works with SWI-Prolog 6.5.3. |
Works with SWI-Prolog 6.5.3. |
||
===All permutations method=== |
===All permutations method=== |
||
< |
<syntaxhighlight lang="prolog">largest_int_v1(In, Out) :- |
||
maplist(name, In, LC), |
maplist(name, In, LC), |
||
aggregate(max(V), get_int(LC, V), Out). |
aggregate(max(V), get_int(LC, V), Out). |
||
Line 1,162: | Line 1,992: | ||
append(P, LV), |
append(P, LV), |
||
name(V, LV). |
name(V, LV). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> ?- largest_int_v1([1, 34, 3, 98, 9, 76, 45, 4], Out). |
<pre> ?- largest_int_v1([1, 34, 3, 98, 9, 76, 45, 4], Out). |
||
Line 1,173: | Line 2,003: | ||
===Method 2=== |
===Method 2=== |
||
< |
<syntaxhighlight lang="prolog">largest_int_v2(In, Out) :- |
||
maplist(name, In, LC), |
maplist(name, In, LC), |
||
predsort(my_sort,LC, LCS), |
predsort(my_sort,LC, LCS), |
||
Line 1,208: | Line 2,038: | ||
my_sort(R, [H1, H1 | T], [H1]) :- |
my_sort(R, [H1, H1 | T], [H1]) :- |
||
my_sort(R, [H1 | T], [H1]) . |
my_sort(R, [H1 | T], [H1]) . |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,222: | Line 2,052: | ||
This also shows one of the few times where cmp= is better than key= on sorted() |
This also shows one of the few times where cmp= is better than key= on sorted() |
||
< |
<syntaxhighlight lang="python">try: |
||
cmp # Python 2 OK or NameError in Python 3 |
cmp # Python 2 OK or NameError in Python 3 |
||
def maxnum(x): |
def maxnum(x): |
||
Line 1,236: | Line 2,066: | ||
key=cmp_to_key(lambda x,y:cmp(y+x, x+y)))) |
key=cmp_to_key(lambda x,y:cmp(y+x, x+y)))) |
||
for numbers in [(1, 34, 3, 98, 9, 76, 45, 4), (54, 546, 548, 60): |
for numbers in [(1, 34, 3, 98, 9, 76, 45, 4), (54, 546, 548, 60)]: |
||
print('Numbers: %r\n Largest integer: %15s' % (numbers, maxnum(numbers)))</ |
print('Numbers: %r\n Largest integer: %15s' % (numbers, maxnum(numbers)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,246: | Line 2,076: | ||
===Python: Compare repeated string method=== |
===Python: Compare repeated string method=== |
||
< |
<syntaxhighlight lang="python">def maxnum(x): |
||
maxlen = len(str(max(x))) |
maxlen = len(str(max(x))) |
||
return ''.join(sorted((str(v) for v in x), reverse=True, |
return ''.join(sorted((str(v) for v in x), reverse=True, |
||
Line 1,252: | Line 2,082: | ||
for numbers in [(212, 21221), (1, 34, 3, 98, 9, 76, 45, 4), (54, 546, 548, 60)]: |
for numbers in [(212, 21221), (1, 34, 3, 98, 9, 76, 45, 4), (54, 546, 548, 60)]: |
||
print('Numbers: %r' % (numbers, maxnum(numbers)))</ |
print('Numbers: %r\n Largest integer: %15s' % (numbers, maxnum(numbers)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,263: | Line 2,093: | ||
{{works with|Python|2.6+}} |
{{works with|Python|2.6+}} |
||
< |
<syntaxhighlight lang="python">from fractions import Fraction |
||
from math import log10 |
from math import log10 |
||
Line 1,271: | Line 2,101: | ||
for numbers in [(1, 34, 3, 98, 9, 76, 45, 4), (54, 546, 548, 60)]: |
for numbers in [(1, 34, 3, 98, 9, 76, 45, 4), (54, 546, 548, 60)]: |
||
print('Numbers: %r\n Largest integer: %15s' % (numbers, maxnum(numbers)))</ |
print('Numbers: %r\n Largest integer: %15s' % (numbers, maxnum(numbers)))</syntaxhighlight> |
||
;Output as first Python example, above. |
;Output as first Python example, above. |
||
===Python: Try all permutations method=== |
===Python: Try all permutations method=== |
||
< |
<syntaxhighlight lang="python">from itertools import permutations |
||
def maxnum(x): |
def maxnum(x): |
||
return max(int(''.join(n) for n in permutations(str(i) for i in x))) |
return max(int(''.join(n) for n in permutations(str(i) for i in x))) |
||
for numbers in [(1, 34, 3, 98, 9, 76, 45, 4), (54, 546, 548, 60)]: |
for numbers in [(1, 34, 3, 98, 9, 76, 45, 4), (54, 546, 548, 60)]: |
||
print('Numbers: %r\n Largest integer: %15s' % (numbers, maxnum(numbers)))</ |
print('Numbers: %r\n Largest integer: %15s' % (numbers, maxnum(numbers)))</syntaxhighlight> |
||
;Output as above. |
;Output as above. |
||
=={{header|Quackery}}== |
|||
===With a string of space separated sequences of digits=== |
|||
<syntaxhighlight lang="quackery">[ sortwith |
|||
[ 2dup swap join |
|||
dip join $< ] |
|||
[] swap witheach join ] is largest-int ( [ --> $ ) |
|||
$ '1 34 3 98 9 76 45 4' nest$ largest-int echo$ cr |
|||
$ '54 546 548 60' nest$ largest-int echo$</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
998764543431 |
|||
6054854654 |
|||
</pre> |
|||
===With a nest of numbers=== |
|||
<syntaxhighlight lang="quackery"> [ number$ dip number$ join $->n drop ] is conc ( n n --> n ) |
|||
[ 2dup conc unrot swap conc < ] is conc> ( n n --> b ) |
|||
[ sortwith conc> |
|||
$ "" swap |
|||
witheach [ number$ join ] |
|||
$->n drop ] is task ( [ --> n ) |
|||
' [ [ 1 34 3 98 9 76 45 4 ] |
|||
[ 54 546 548 60 ] ] |
|||
witheach [ task echo cr ] </syntaxhighlight> |
|||
{{out}} |
|||
<pre>998764543431 |
|||
6054854654</pre> |
|||
=={{header|R}}== |
|||
<syntaxhighlight lang="r">Largest_int_from_concat_ints <- function(vec){ |
|||
#recursive function for computing all permutations |
|||
perm <- function(vec) { |
|||
n <- length(vec) |
|||
if (n == 1) |
|||
return(vec) |
|||
else { |
|||
x <- NULL |
|||
for (i in 1:n){ |
|||
x <- rbind(x, cbind(vec[i], perm(vec[-i]))) |
|||
} |
|||
return(x) |
|||
} |
|||
} |
|||
permutations <- perm(vec) |
|||
concat <- as.numeric(apply(permutations, 1, paste, collapse = "")) |
|||
return(max(concat)) |
|||
} |
|||
#Verify |
|||
Largest_int_from_concat_ints(c(54, 546, 548, 60)) |
|||
Largest_int_from_concat_ints(c(1, 34, 3, 98, 9, 76, 45, 4)) |
|||
Largest_int_from_concat_ints(c(93, 4, 89, 21, 73)) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[1] 6054854654 |
|||
[1] 998764543431 |
|||
[1] 938973421 |
|||
</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang="racket"> |
|||
<lang Racket> |
|||
#lang racket |
#lang racket |
||
(define (largest-int ns) |
(define (largest-int ns) |
||
Line 1,293: | Line 2,196: | ||
(map largest-int '((1 34 3 98 9 76 45 4) (54 546 548 60))) |
(map largest-int '((1 34 3 98 9 76 45 4) (54 546 548 60))) |
||
;; -> '(998764543431 6054854654) |
;; -> '(998764543431 6054854654) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
<syntaxhighlight lang="raku" line>sub maxnum(*@x) { |
|||
[~] @x.sort: -> $a, $b { $b ~ $a leg $a ~ $b } |
|||
} |
|||
say maxnum <1 34 3 98 9 76 45 4>; |
|||
say maxnum <54 546 548 60>;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>998764543431 |
|||
6054854654</pre> |
|||
=={{header|Red}}== |
|||
<syntaxhighlight lang="rebol">Red [] |
|||
foreach seq [[1 34 3 98 9 76 45 4] [54 546 548 60]] [ |
|||
print rejoin sort/compare seq function [a b] [ (rejoin [a b]) > rejoin [b a] ] |
|||
] |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
998764543431 |
|||
6054854654</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
The algorithm used is based on exact comparisons (left to right) with right digit fill of the |
The algorithm used is based on exact comparisons (left to right) with ''right digit fill'' of the ''left digit''. |
||
<br>This allows the integers to be of any size. |
|||
This REXX version works with any integer (negative, zero, positive), and does some basic error checking to |
This REXX version works with any size integer (negative, zero, positive), and does some basic error checking to |
||
<br>verify that the numbers are indeed integers (and it also normalizes the integers). |
|||
<lang rexx>/*REXX pgm constructs largest integer from a list using concatenation.*/ |
|||
@. = /*used to signify end-of-lists. */ |
|||
@.1 = '{1, 34, 3, 98, 9, 76, 45, 4}' /*the first list of integers. */ |
|||
@.2 = '{54, 546, 548, 60}' /* " second " " " */ |
|||
@.3 = '{ 4, 45, 54, 5}' /* " third " " " */ |
|||
/* [↓] process all the lists. */ |
|||
do j=1 while @.j\==''; $= /*keep truckin' until exhausted. */ |
|||
z=space(translate(@.j, , '])},{([')) /*perform scrubbing on the list. */ |
|||
_=length(space(z,0)) + 1 /*determine the largest possible#*/ |
|||
if _>digits() then numeric digits _ /*ensure 'nuff digits for the #. */ |
|||
/* [↓] examine each num in list.*/ |
|||
do while z\==''; index=1 /*keep examining list until done.*/ |
|||
big=isOK(word(z,1)) /*assume first number is biggest.*/ |
|||
The absolute value is used for negative numbers. No sorting of the numbers is required for the 1<sup>st</sup> two examples. |
|||
do k=2 to words(z); x=isOK(word(z,k)) /*get an integer.*/ |
|||
===simple integers=== |
|||
x1=left(x,1); L=max(length(big), length(x)) /*get max length.*/ |
|||
<syntaxhighlight lang="rexx">/*REXX program constructs the largest integer from an integer list using concatenation.*/ |
|||
if left(x, L, x1) <<= left(big, L, left(big,1)) then iterate |
|||
@.=.; @.1 = 1 34 3 98 9 76 45 4 /*the 1st integer list to be used. */ |
|||
@.2 = 54 546 548 60 /* " 2nd " " " " " */ |
|||
@.3 = 4 45 54 5 /* " 3rd " " " " " */ |
|||
w=0 /* [↓] process all the integer lists.*/ |
|||
do j=1 while @.j\==.; z= space(@.j) /*keep truckin' until lists exhausted. */ |
|||
w=max(w, length(z) ); $= /*obtain maximum width to align output.*/ |
|||
do while z\=''; idx= 1; big= norm(1) /*keep examining the list until done.*/ |
|||
do k=2 to words(z); #= norm(k) /*obtain an a number from the list. */ |
|||
L= max(length(big), length(#) ) /*get the maximum length of the integer*/ |
|||
if left(#, L, left(#, 1) ) <<= left(big, L, left(big, 1) ) then iterate |
|||
big= #; idx= k /*we found a new biggie (and the index)*/ |
|||
end /*k*/ /* [↑] find max concatenated integer. */ |
|||
z= delword(z, idx, 1) /*delete this maximum integer from list*/ |
|||
$= $ || big /*append " " " ───► $. */ |
|||
end /*while z*/ /* [↑] process all integers in a list.*/ |
|||
say 'largest concatenatated integer from ' left( space(@.j), w) " is ─────► " $ |
|||
end /*j*/ /* [↑] process each list of integers. */ |
|||
exit 0 /*stick a fork in it, we're all done. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
norm: arg i; #= word(z, i); er= '***error***'; if left(#, 1)=="-" then #= substr(#, 2) |
|||
if \datatype(#,'W') then do; say er # "isn't an integer."; exit 13; end; return #/1</syntaxhighlight> |
|||
{{out|output|text= when using the default (internal) integer lists:}} |
|||
<pre> |
|||
largest concatenatated integer from 1 34 3 98 9 76 45 4 is ─────► 998764543431 |
|||
largest concatenatated integer from 54 546 548 60 is ─────► 6054854654 |
|||
largest concatenatated integer from 4 45 54 5 is ─────► 554454 |
|||
</pre> |
|||
===exponentiated integers=== |
|||
z=space(delword(z, index, 1)) /*remove the "maximum" from list.*/ |
|||
In REXX, a number such as '''6.6e77''' would be considered an integer ''if'' the (current) '''numeric digits''' is |
|||
$=$ || big /*append the "maximum" number. */ |
|||
<br>large enough to express that number as an integer without the exponent. |
|||
end /*while z ···*/ /* [↑] process all nums in list.*/ |
|||
The default for REXX is '''9''' decimal digits, but the '''norm''' function automatically uses enough decimal digits to |
|||
<br>express the number as an integer. |
|||
This REXX version can handle any sized integer (most REXXes can handle up to around eight million decimal |
|||
<br>digits, but displaying the result would be problematic for results wider than the display area). |
|||
<syntaxhighlight lang="rexx">/*REXX program constructs the largest integer from an integer list using concatenation.*/ |
|||
@.=.; @.1 = 1 34 3 98 9 76 45 4 /*the 1st integer list to be used. */ |
|||
@.2 = 54 546 548 60 /* " 2nd " " " " " */ |
|||
@.3 = 4 45 54 5 /* " 3rd " " " " " */ |
|||
@.4 = 4 45 54 5 6.6e77 /* " 4th " " " " " */ |
|||
w= 0 /* [↓] process all the integer lists.*/ |
|||
do j=1 while @.j\==.; z= space(@.j) /*keep truckin' until lists exhausted. */ |
|||
w=max(w, length(z) ); $= /*obtain maximum width to align output.*/ |
|||
do while z\=''; idx=1; big= norm(1) /*keep examining the list until done.*/ |
|||
do k=2 to words(z); #= norm(k) /*obtain an a number from the list. */ |
|||
L= max(length(big), length(#) ) /*get the maximum length of the integer*/ |
|||
if left(#, L, left(#, 1) ) <<= left(big, L, left(big, 1) ) then iterate |
|||
big=#; idx= k /*we found a new biggie (and the index)*/ |
|||
end /*k*/ /* [↑] find max concatenated integer. */ |
|||
z= delword(z, idx, 1) /*delete this maximum integer from list*/ |
|||
$= $ || big /*append " " " ───► $. */ |
|||
end /*while z*/ /* [↑] process all integers in a list.*/ |
|||
say 'largest concatenatated integer from ' left( space(@.j), w) " is " $ |
|||
end /*j*/ /* [↑] process each list of integers. */ |
|||
exit /*stick a fork in it, we're all done. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
norm: arg i; #= word(z, i); er= '***error***'; if left(#, 1)=="-" then #= substr(#, 2) |
|||
if \datatype(#, 'N') then signal er13 /*go and tell err msg.*/ |
|||
else #= # / 1 /*a #, so normalize it*/ |
|||
if pos('E',#)>0 then do; parse var # mant "E" pow /*Has exponent? Expand*/ |
|||
numeric digits pow + length(mand) /*expand digs, adjust#*/ |
|||
end |
|||
if datatype(#, 'W') then return # / 1 |
|||
er13: say er # "isn't an integer."; exit 13</syntaxhighlight> |
|||
{{out|output|text= when using the default (internal) integer lists:}} |
|||
(Output shown at three-quarter size.) |
|||
<pre style="font-size:75%"> |
|||
largest concatenatated integer from 1 34 3 98 9 76 45 4 is 998764543431 |
|||
largest concatenatated integer from 54 546 548 60 is 6054854654 |
|||
largest concatenatated integer from 4 45 54 5 is 554454 |
|||
largest concatenatated integer from 4 45 54 5 6.6e77 is 660000000000000000000000000000000000000000000000000000000000000000000000000000554454 |
|||
</pre> |
|||
===Alternate Version=== |
|||
Inspired by the previous versions. |
|||
<syntaxhighlight lang="text">/*REXX program constructs the largest integer from an integer list using concatenation.*/ |
|||
l.=''; l.1 = '1 34 3 98 9 76 45 4' /*the 1st integer list to be used. */ |
|||
l.2 = '54 546 548 60' /* " 2nd " " " " " */ |
|||
l.3 = ' 4 45 54 5' /* " 3rd " " " " " */ |
|||
l.4 = ' 4 45 54 5 6.6e77' /* " 4th " " " " " */ |
|||
l.5 = ' 3 3 .2' /* " 5th " " " " " */ |
|||
/* |
|||
soll.1=998764543431 |
|||
soll.2=6054854654 |
|||
soll.3=554454 |
|||
soll.4=660000000000000000000000000000000000000000000000000000000000000000000000000000545454 |
|||
*/ |
|||
l_length=0 |
|||
Do li=1 By 1 While l.li<>'' |
|||
l_length=max(l_length,length(space(l.li))) |
|||
End |
|||
Do li=1 By 1 While l.li<>'' |
|||
z='' |
|||
Do j=1 To words(l.li) |
|||
int=integer(word(l.li,j)) |
|||
If int='?' Then Do |
|||
Say left(space(l.li),l_length) '-> ** invalid ** bad integer' word(l.li,j) |
|||
Iterate li |
|||
End |
|||
Else |
|||
z=z int |
|||
End |
|||
/*Say copies(' ',l_length) ' ' soll.li */ |
|||
Say left(space(l.li),l_length) '->' largeint(l.li) |
|||
End |
|||
Exit |
|||
integer: Procedure |
|||
Numeric Digits 1000 |
|||
Parse Arg z |
|||
If Datatype(z,'W') Then |
|||
Return z+0 |
|||
Else |
|||
Return '?' |
|||
largeint: |
|||
result='' |
|||
Do While z<>'' /* [?] check the rest of the integers.*/ |
|||
big=word(z,1); index=1; LB=length(big) /*assume that first integer is biggest.*/ |
|||
do k=2 to words(z); |
|||
n=word(z,k) /*obtain an integer from the list. */ |
|||
L=max(LB,length(n)) /*get the maximum length of the integer*/ |
|||
if left(n,L,left(n,1))<<=left(big,L,left(big,1)) then iterate |
|||
big=n; index=k /*we found a new biggie (and the index)*/ |
|||
LB=length(big) |
|||
End /*k*/ |
|||
z=delword(z,index,1) /*delete this maximum integer from list*/ |
|||
result=result||big /*append " " " ---? $. */ |
|||
end /*while z*/ /* [?] process all integers in a list.*/ |
|||
Return result</syntaxhighlight> |
|||
{{out}} |
|||
<pre>1 34 3 98 9 76 45 4 -> 998764543431 |
|||
54 546 548 60 -> 6054854654 |
|||
4 45 54 5 -> 554454 |
|||
4 45 54 5 6.6e77 -> 660000000000000000000000000000000000000000000000000000000000000000000000000000554454 |
|||
3 3 .2 -> ** invalid ** bad integer .2</pre> |
|||
===Version 4=== |
|||
{{trans|NetRexx}} |
|||
<syntaxhighlight lang="rexx">/*REXX program constructs the largest integer from an integer list using concatenation.*/ |
|||
l.=''; l.1 = '1 34 3 98 9 76 45 4' /*the 1st integer list to be used. */ |
|||
l.2 = '54 546 548 60' /* " 2nd " " " " " */ |
|||
l.3 = ' 4 45 54 5' /* " 3rd " " " " " */ |
|||
l.4 = ' 4 45 54 5 6.6e77' /* " 4th " " " " " */ |
|||
l.5 = ' 3 3 .2' /* " 5th " " " " " */ |
|||
l.6 = ' 4 45 54 5 6.6e1001' /* " 6th " " " " " */ |
|||
l.7 = ' 4.0000 45 54 5.00' /* " 7th " " " " " */ |
|||
l.8 = ' 10e999999999 5' /* " 8th " " " " " */ |
|||
l_length=0 |
|||
Do li=1 By 1 While l.li<>'' |
|||
l_length=max(l_length,length(space(l.li))) |
|||
End |
|||
Do li=1 By 1 While l.li<>'' |
|||
z='' |
|||
msg='' |
|||
Do j=1 To words(l.li) |
|||
int=integer(word(l.li,j)) |
|||
If int='?' Then Do |
|||
Say left(space(l.li),l_length) '-> ** invalid ** bad list item:' word(l.li,j) msg |
|||
Iterate li |
|||
End |
|||
Else |
|||
z=z int |
|||
End |
|||
zz=largeint(z) |
|||
If length(zz)<60 Then |
|||
Say left(space(l.li),l_length) '->' zz |
|||
Else |
|||
Say left(space(l.li),l_length) '->' left(zz,5)'...'right(zz,5) |
|||
End |
|||
Exit |
|||
integer: Procedure Expose msg |
|||
Numeric Digits 1000 |
|||
Parse Arg z |
|||
If Datatype(z,'W') Then |
|||
Return z/1 |
|||
Else Do |
|||
If Datatype(z,'NUM') Then Do |
|||
Do i=1 To 6 Until dig>=999999999 |
|||
dig= digits()*10 |
|||
dig=min(dig,999999999) |
|||
Numeric Digits dig |
|||
If Datatype(z,'W') Then |
|||
Return z/1 |
|||
End |
|||
msg='cannot convert it to an integer' |
|||
Return '?' |
|||
End |
|||
Else Do |
|||
msg='not a number (larger than what this REXX can handle)' |
|||
Return '?' |
|||
End |
|||
End |
|||
largeint: Procedure |
|||
Parse Arg list |
|||
w.0=words(list) |
|||
Do i=1 To w.0 |
|||
w.i=word(list,i) |
|||
End |
|||
Do wx=1 To w.0-1 |
|||
Do wy=wx+1 To w.0 |
|||
xx=w.wx |
|||
yy=w.wy |
|||
xy=xx||yy |
|||
yx=yy||xx |
|||
if xy < yx then do |
|||
/* swap xx and yy */ |
|||
w.wx = yy |
|||
w.wy = xx |
|||
end |
|||
End |
|||
End |
|||
list='' |
|||
Do ww=1 To w.0 |
|||
list=list w.ww |
|||
End |
|||
Return space(list,0)</syntaxhighlight> |
|||
{{out}} |
|||
<pre>1 34 3 98 9 76 45 4 -> 998764543431 |
|||
54 546 548 60 -> 6054854654 |
|||
4 45 54 5 -> 554454 |
|||
4 45 54 5 6.6e77 -> 66000...54454 |
|||
3 3 .2 -> ** invalid ** bad list item: .2 cannot convert it to an integer |
|||
4 45 54 5 6.6e1001 -> 66000...54454 |
|||
4.0000 45 54 5.00 -> 554454 |
|||
10e999999999 5 -> ** invalid ** bad list item: 10e999999999 not a number (larger than what this REXX can handle)</pre> |
|||
=={{header|Ring}}== |
|||
<syntaxhighlight lang="ring"> |
|||
nums=[1,34,3,98,9,76,45,4] |
|||
see largestInt(8) + nl |
|||
nums=[54,546,548,60] |
|||
see largestInt(4) + nl |
|||
func largestInt len |
|||
l = "" |
|||
sorted = false |
|||
while not sorted |
|||
sorted=true |
|||
for i=1 to len-1 |
|||
a=string(nums[i]) |
|||
b=string(nums[i+1]) |
|||
if a+b<b+a |
|||
temp = nums[i] |
|||
nums[i] = nums[i+1] |
|||
nums[i+1] = temp |
|||
sorted=false ok |
|||
next |
|||
end |
|||
for i=1 to len |
|||
l+=string(nums[i]) |
|||
next |
|||
return l |
|||
</syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
998764543431 |
|||
6054854654 |
|||
</pre> |
|||
=={{header|RPL}}== |
|||
say right($,digits()) ' max for: ' @.j /*show max integer & list.*/ |
|||
We use here the second algorithm, easily derived from the SORT program given in [[Sorting algorithms/Bubble sort#RPL|Sorting algorithms/Bubble sort]]. |
|||
end /*j*/ /* [↑] process each list of nums*/ |
|||
{{works with|HP|28}} |
|||
≪ LIST→ → len |
|||
≪ 1 len '''START''' |
|||
→STR len ROLL '''END''' |
|||
len 1 '''FOR''' n |
|||
1 n 1 - '''START''' |
|||
'''IF''' DUP2 + LAST SWAP + < '''THEN''' SWAP '''END''' |
|||
n ROLLD |
|||
'''NEXT''' n ROLLD |
|||
-1 '''STEP''' |
|||
2 len '''START''' + '''END''' STR→ |
|||
≫ ≫ ‘<span style="color:blue">MKBIG</span>’ STO |
|||
{212 21221} <span style="color:blue">MKBIG</span> |
|||
exit /*stick a fork in it, we're done.*/ |
|||
{1 34 3 98 9 76 45 4} <span style="color:blue">MKBIG</span> |
|||
/*───────────────────────────────────ISOK subroutine────────────────────*/ |
|||
{54 546 548 60} <span style="color:blue">MKBIG</span> |
|||
isOK: parse arg ?; if datatype(?,'W') then return abs(?)/1 /*normalize*/ |
|||
say; say '***error!*** number ' ? "isn't an integer."; say; exit 13</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
3: 21221221 |
|||
998764543431 max for: {1, 34, 3, 98, 9, 76, 45, 4} |
|||
2: 998764543431 |
|||
6054854654 max for: {54, 546, 548, 60} |
|||
1: 6054854654 |
|||
554454 max for: { 4, 45, 54, 5} |
|||
</pre> |
</pre> |
||
Line 1,342: | Line 2,528: | ||
{{trans|Tcl}} |
{{trans|Tcl}} |
||
< |
<syntaxhighlight lang="ruby">def icsort nums |
||
nums.sort { |x, y| "#{y}#{x}" <=> "#{x}#{y}" } |
nums.sort { |x, y| "#{y}#{x}" <=> "#{x}#{y}" } |
||
end |
end |
||
Line 1,349: | Line 2,535: | ||
p c # prints nicer in Ruby 1.8 |
p c # prints nicer in Ruby 1.8 |
||
puts icsort(c).join |
puts icsort(c).join |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,359: | Line 2,545: | ||
===Compare repeated string method=== |
===Compare repeated string method=== |
||
< |
<syntaxhighlight lang="ruby">def icsort nums |
||
maxlen = nums.max.to_s.length |
maxlen = nums.max.to_s.length |
||
nums.map{ |x| x.to_s }.sort_by { |x| x * (maxlen * 2 / x.length) }.reverse |
nums.map{ |x| x.to_s }.sort_by { |x| x * (maxlen * 2 / x.length) }.reverse |
||
Line 1,367: | Line 2,553: | ||
p c # prints nicer in Ruby 1.8 |
p c # prints nicer in Ruby 1.8 |
||
puts icsort(c).join |
puts icsort(c).join |
||
end</ |
end</syntaxhighlight> |
||
;Output as above. |
;Output as above. |
||
< |
<syntaxhighlight lang="ruby">require 'rational' #Only needed in Ruby < 1.9 |
||
def icsort nums |
def icsort nums |
||
Line 1,380: | Line 2,566: | ||
p c # prints nicer in Ruby 1.8 |
p c # prints nicer in Ruby 1.8 |
||
puts icsort(c).join |
puts icsort(c).join |
||
end</ |
end</syntaxhighlight> |
||
;Output as above. |
;Output as above. |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">a1$ = "1, 34, 3, 98, 9, 76, 45, 4" |
||
a2$ = "54,546,548,60" |
a2$ = "54,546,548,60" |
||
Line 1,413: | Line 2,599: | ||
maxNum$ = maxNum$ ; a$(j) |
maxNum$ = maxNum$ ; a$(j) |
||
next j |
next j |
||
end function</ |
end function</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Max Num 1, 34, 3, 98, 9, 76, 45, 4 = 998764543431 |
<pre>Max Num 1, 34, 3, 98, 9, 76, 45, 4 = 998764543431 |
||
Max Num 54,546,548,60 = 6054854654</pre> |
Max Num 54,546,548,60 = 6054854654</pre> |
||
=={{header|Rust}}== |
|||
<syntaxhighlight lang="rust">fn maxcat(a: &mut [u32]) { |
|||
a.sort_by(|x, y| { |
|||
let xy = format!("{}{}", x, y); |
|||
let yx = format!("{}{}", y, x); |
|||
xy.cmp(&yx).reverse() |
|||
}); |
|||
for x in a { |
|||
print!("{}", x); |
|||
} |
|||
println!(); |
|||
} |
|||
fn main() { |
|||
maxcat(&mut [1, 34, 3, 98, 9, 76, 45, 4]); |
|||
maxcat(&mut [54, 546, 548, 60]); |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre>998764543431 |
|||
6054854654</pre> |
|||
=={{header|S-lang}}== |
|||
<syntaxhighlight lang="s-lang">define catcmp(a, b) |
|||
{ |
|||
a = string(a); |
|||
b = string(b); |
|||
return strcmp(b+a, a+b); |
|||
} |
|||
define maxcat(arr) |
|||
{ |
|||
arr = arr[array_sort(arr, &catcmp)]; |
|||
variable result = "", elem; |
|||
foreach elem (arr) |
|||
result += string(elem); |
|||
return result; |
|||
} |
|||
print("max of series 1 is " + maxcat([1, 34, 3, 98, 9, 76, 45, 4])); |
|||
print("max of series 2 is " + maxcat([54, 546, 548, 60])); |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>"max of series 1 is 998764543431" |
|||
"max of series 2 is 6054854654" |
|||
</pre> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{libheader|Scala}} |
{{libheader|Scala}} |
||
< |
<syntaxhighlight lang="scala">object LIFCI extends App { |
||
def lifci(list: List[Long]) = list.permutations.map(_.mkString).max |
def lifci(list: List[Long]) = list.permutations.map(_.mkString).max |
||
Line 1,426: | Line 2,658: | ||
println(lifci(List(1, 34, 3, 98, 9, 76, 45, 4))) |
println(lifci(List(1, 34, 3, 98, 9, 76, 45, 4))) |
||
println(lifci(List(54, 546, 548, 60))) |
println(lifci(List(54, 546, 548, 60))) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,435: | Line 2,667: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
< |
<syntaxhighlight lang="scheme">(define (cat . nums) (apply string-append (map number->string nums))) |
||
(define (my-compare a b) (string>? (cat a b) (cat b a))) |
(define (my-compare a b) (string>? (cat a b) (cat b a))) |
||
(map (lambda (xs) (string->number (apply cat (sort xs my-compare)))) |
(map (lambda (xs) (string->number (apply cat (sort xs my-compare)))) |
||
'((1 34 3 98 9 76 45 4) (54 546 548 60)))</ |
'((1 34 3 98 9 76 45 4) (54 546 548 60)))</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
<pre> |
<pre> |
||
Line 1,448: | Line 2,680: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="ruby">func maxnum(nums) { |
||
nums.sort {|x,y| "#{y}#{x}" <=> "#{x}#{y}" }; |
nums.sort {|x,y| "#{y}#{x}" <=> "#{x}#{y}" }; |
||
} |
} |
||
Line 1,454: | Line 2,686: | ||
[[54, 546, 548, 60], [1, 34, 3, 98, 9, 76, 45, 4]].each { |c| |
[[54, 546, 548, 60], [1, 34, 3, 98, 9, 76, 45, 4]].each { |c| |
||
say maxnum(c).join.to_num; |
say maxnum(c).join.to_num; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,460: | Line 2,692: | ||
998764543431 |
998764543431 |
||
</pre> |
</pre> |
||
=={{header|Smalltalk}}== |
|||
Version 1) sort by padded print strings: |
|||
{{works with|Smalltalk/X}} |
|||
<syntaxhighlight lang="smalltalk">#( |
|||
(54 546 548 60) |
|||
(1 34 3 98 9 76 45 4) |
|||
) do:[:ints | |
|||
|resultString| |
|||
"sort ints by padded strings (sort a copy - literals are immudatble), |
|||
then collect their strings, then concatenate" |
|||
resultString := |
|||
((ints copy sort:[:a :b | |
|||
|pad| |
|||
pad := (a integerLog10) max:(b integerLog10). |
|||
(a printString paddedTo:pad with:$0) > (b printString paddedTo:pad with:$0)]) |
|||
collect:#printString) asStringWith:''. |
|||
Stdout printCR: resultString. |
|||
].</syntaxhighlight> |
|||
Version 2) alternative: sort by concatenated pair's strings: |
|||
<syntaxhighlight lang="smalltalk">#( |
|||
(54 546 548 60) |
|||
(1 34 3 98 9 76 45 4) |
|||
) do:[:ints | |
|||
|resultString| |
|||
resultString := |
|||
((ints copy sort:[:a :b | e'{a}{b}' > e'{b}{a}']) "(1)" |
|||
collect:#printString) asStringWith:''. |
|||
Stdout printCR: resultString. |
|||
].</syntaxhighlight> |
|||
Note ¹ replace "e'{a}{b}'" by "(a printString,b printString)" in dialects, which do not support embedded expression strings. |
|||
Version 3) no need to collect the resultString; simply print the sorted list (ok, if printing is all we want): |
|||
<syntaxhighlight lang="smalltalk">#( |
|||
(54 546 548 60) |
|||
(1 34 3 98 9 76 45 4) |
|||
) do:[:ints | |
|||
(ints copy sort:[:a :b | e'{a}{b}' > e'{b}{a}']) |
|||
do:[:eachNr | eachNr printOn:Stdout]. |
|||
Stdout cr. |
|||
]</syntaxhighlight> |
|||
Version 4) no need to generate any intermediate strings; the following will do as well: |
|||
<syntaxhighlight lang="smalltalk">#( |
|||
(54 546 548 60) |
|||
(1 34 3 98 9 76 45 4) |
|||
) do:[:ints | |
|||
(ints copy sortByApplying:[:i | i log10 fractionPart]) reverseDo:#print. |
|||
Stdout cr. |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>6054854654 |
|||
989764543431</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc intcatsort {nums} { |
||
lsort -command {apply {{x y} {expr {"$y$x" - "$x$y"}}}} $nums |
lsort -command {apply {{x y} {expr {"$y$x" - "$x$y"}}}} $nums |
||
}</ |
}</syntaxhighlight> |
||
Demonstrating: |
Demonstrating: |
||
< |
<syntaxhighlight lang="tcl">foreach collection { |
||
{1 34 3 98 9 76 45 4} |
{1 34 3 98 9 76 45 4} |
||
{54 546 548 60} |
{54 546 548 60} |
||
Line 1,472: | Line 2,759: | ||
set sorted [intcatsort $collection] |
set sorted [intcatsort $collection] |
||
puts "\[$collection\] => \[$sorted\] (concatenated: [join $sorted ""])" |
puts "\[$collection\] => \[$sorted\] (concatenated: [join $sorted ""])" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
[1 34 3 98 9 76 45 4] => [9 98 76 45 4 34 3 1] (concatenated: 998764543431) |
[1 34 3 98 9 76 45 4] => [9 98 76 45 4 34 3 1] (concatenated: 998764543431) |
||
[54 546 548 60] => [60 548 546 54] (concatenated: 6054854654) |
[54 546 548 60] => [60 548 546 54] (concatenated: 6054854654) |
||
</pre> |
|||
=={{header|Transd}}== |
|||
<syntaxhighlight lang="Scheme">#lang transd |
|||
MainModule: { |
|||
_start: (lambda |
|||
(for ar in [[98, 76, 45, 34, 9, 4, 3, 1], [54, 546, 548, 60]] do |
|||
(sort ar (λ l Int() r Int() (ret (> Int(String(l r)) Int(String(r l)))))) |
|||
(lout (join ar ""))) |
|||
) |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
998764543431 |
|||
6054854654 |
|||
</pre> |
|||
=={{header|Uiua}}== |
|||
<syntaxhighlight lang="uiua"> |
|||
A ← {[212 21221] [1 34 3 98 9 76 45 4][54 546 548 60]} |
|||
≡(⋕/◇⊂⊏⊸(⍖≡↯[/↥≡⧻]).°⋕)A |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[21221212 989764543431 6054854654] |
|||
</pre> |
|||
=={{header|VBScript}}== |
|||
{{trans|BBC BASIC}} |
|||
<syntaxhighlight lang="vb"> |
|||
Function largestint(list) |
|||
nums = Split(list,",") |
|||
Do Until IsSorted = True |
|||
IsSorted = True |
|||
For i = 0 To UBound(nums) |
|||
If i <> UBound(nums) Then |
|||
a = nums(i) |
|||
b = nums(i+1) |
|||
If CLng(a&b) < CLng(b&a) Then |
|||
tmpnum = nums(i) |
|||
nums(i) = nums(i+1) |
|||
nums(i+1) = tmpnum |
|||
IsSorted = False |
|||
End If |
|||
End If |
|||
Next |
|||
Loop |
|||
For j = 0 To UBound(nums) |
|||
largestint = largestint & nums(j) |
|||
Next |
|||
End Function |
|||
WScript.StdOut.Write largestint(WScript.Arguments(0)) |
|||
WScript.StdOut.WriteLine |
|||
</syntaxhighlight> |
|||
{{Out}} |
|||
<pre> |
|||
F:\>cscript /nologo largestint.vbs 1,34,3,98,9,76,45,4 |
|||
998764543431 |
|||
F:\>cscript /nologo largestint.vbs 54,546,548,60 |
|||
6054854654 |
|||
</pre> |
</pre> |
||
=={{header|Vim Script}}== |
=={{header|Vim Script}}== |
||
This solution is intended to be run as an Ex command within a buffer containing the integers to be processed, one per line. |
This solution is intended to be run as an Ex command within a buffer containing the integers to be processed, one per line. |
||
< |
<syntaxhighlight lang="vim">%s/\(.\+\)/\1\1/ | sort! | %s/\(.\+\)\1\n/\1/</syntaxhighlight> |
||
;Demonstration |
;Demonstration |
||
< |
<syntaxhighlight lang="bash">$ paste -s nums |
||
1 34 3 98 9 76 45 4 |
1 34 3 98 9 76 45 4 |
||
$ vim -S icsort.vim nums |
$ vim -S icsort.vim nums |
||
998764543431</ |
998764543431</syntaxhighlight> |
||
=={{header|Wren}}== |
|||
{{trans|Kotlin}} |
|||
{{libheader|Wren-sort}} |
|||
<syntaxhighlight lang="wren">import "./sort" for Sort |
|||
var cmp = Fn.new { |x, y| |
|||
var xy = Num.fromString(x.toString + y.toString) |
|||
var yx = Num.fromString(y.toString + x.toString) |
|||
return (xy - yx).sign |
|||
} |
|||
var findLargestSequence = Fn.new { |a| |
|||
var b = Sort.merge(a, cmp) |
|||
return b[-1..0].join() |
|||
} |
|||
var arrays = [ |
|||
[1, 34, 3, 98, 9, 76, 45, 4], |
|||
[54, 546, 548, 60] |
|||
] |
|||
for (a in arrays) { |
|||
System.print("%(a) -> %(findLargestSequence.call(a))") |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[1, 34, 3, 98, 9, 76, 45, 4] -> 998764543431 |
|||
[54, 546, 548, 60] -> 6054854654 |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn bigCI(ns){ |
||
ns.apply("toString").sort(fcn(a,b){ (a+b)>(b+a) }).concat(); |
ns.apply("toString").sort(fcn(a,b){ (a+b)>(b+a) }).concat(); |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">bigCI(T(1, 34, 3, 98, 9, 76, 45, 4)).println(); |
||
bigCI(T(54, 546, 548, 60)).println();</ |
bigCI(T(54, 546, 548, 60)).println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
Latest revision as of 22:48, 22 June 2024
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
Given a set of positive integers, write a function to order the integers in such a way that the concatenation of the numbers forms the largest possible integer and return this integer.
Use the following two sets of integers as tests and show your program output here.
- {1, 34, 3, 98, 9, 76, 45, 4}
- {54, 546, 548, 60}
- Possible algorithms
- A solution could be found by trying all combinations and return the best.
- Another way to solve this is to note that in the best arrangement, for any two adjacent original integers X and Y, the concatenation X followed by Y will be numerically greater than or equal to the concatenation Y followed by X.
- Yet another way to solve this is to pad the integers to the same size by repeating the digits then sort using these repeated integers as a sort key.
- See also
- Algorithms: What is the most efficient way to arrange the given numbers to form the biggest number?
- Constructing the largest number possible by rearranging a list
11l
F maxnum(x)
V maxlen = String(max(x)).len
R sorted(x.map(v -> String(v)), key' i -> i * (@maxlen * 2 I/ i.len), reverse' 1B).join(‘’)
L(numbers) [[212, 21221], [1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60]]
print("Numbers: #.\n Largest integer: #15".format(numbers, maxnum(numbers)))
- Output:
Numbers: [212, 21221] Largest integer: 21221221 Numbers: [1, 34, 3, 98, 9, 76, 45, 4] Largest integer: 998764543431 Numbers: [54, 546, 548, 60] Largest integer: 6054854654
Ada
The algorithmic idea is to apply a twisted comparison function:
function Order(Left, Right: Natural) return Boolean is
( (Img(Left) & Img(Right)) > (Img(Right) & Img(Left)) );
This function converts the parameters Left and Right to strings and returns True if (Left before Right) exceeds (Right before Left). It needs Ada 2012 -- the code for older versions of Ada would be more verbose.
The rest is straightforward: Run your favourite sorting subprogram that allows to use the function "Order" instead of standard comparison operators ("<" or ">" or so) and print the results:
with Ada.Text_IO, Ada.Containers.Generic_Array_Sort;
procedure Largest_Int_From_List is
function Img(N: Natural) return String is
S: String := Integer'Image(N);
begin
return S(S'First+1 .. S'Last); -- First character is ' '
end Img;
function Order(Left, Right: Natural) return Boolean is
( (Img(Left) & Img(Right)) > (Img(Right) & Img(Left)) );
type Arr_T is array(Positive range <>) of Natural;
procedure Sort is new Ada.Containers.Generic_Array_Sort
(Positive, Natural, Arr_T, Order);
procedure Print_Sorted(A: Arr_T) is
B: Arr_T := A;
begin
Sort(B);
for Number of B loop
Ada.Text_IO.Put(Img(Number));
end loop;
Ada.Text_IO.New_Line;
end Print_Sorted;
begin
Print_Sorted((1, 34, 3, 98, 9, 76, 45, 4));
Print_Sorted((54, 546, 548, 60));
end Largest_Int_From_List;
Aime
largest(...)
{
index x;
for (, integer e in xcall(list).__list) {
x[999999999 - 9.times(b_, data(), e).b_size(9).atoi] = e;
}
x.ucall(o_, 0);
o_newline();
}
main(void)
{
largest(1, 34, 3, 98, 9, 76, 45, 4);
largest(54, 546, 548, 60);
0;
}
works for input up to 999999999.
- Output:
998764543431 6054854654
ALGOL 68
Using method 2 - first sorting into first digit order and then comparing concatenated pairs.
BEGIN
# returns the integer value of s #
OP TOINT = ( STRING s)INT:
BEGIN
INT result := 0;
FOR s pos FROM LWB s TO UPB s DO
result *:= 10 +:= ( ABS s[ s pos ] - ABS "0" )
OD;
result
END # TOINT # ;
# returns the first digit of n #
OP FIRSTDIGIT = ( INT n )INT:
BEGIN
INT result := ABS n;
WHILE result > 9 DO result OVERAB 10 OD;
result
END # FIRSTDIGIT # ;
# returns a string representaton of n #
OP TOSTRING = ( INT n )STRING: whole( n, 0 );
# returns an array containing the values of a sorted such that concatenating the values would result in the largest value #
OP CONCATSORT = ( []INT a )[]INT:
IF LWB a >= UPB a THEN
# 0 or 1 element(s) #
a
ELSE
# 2 or more elements #
[ 1 : ( UPB a - LWB a ) + 1 ]INT result := a[ AT 1 ];
# sort the numbers into reverse first digit order #
FOR o pos FROM UPB result - 1 BY -1 TO 1
WHILE BOOL swapped := FALSE;
FOR i pos TO o pos DO
IF FIRSTDIGIT result[ i pos ] < FIRSTDIGIT result[ i pos + 1 ] THEN
INT t = result[ i pos + 1 ];
result[ i pos + 1 ] := result[ i pos ];
result[ i pos ] := t;
swapped := TRUE
FI
OD;
swapped
DO SKIP OD;
# now re-order adjacent numbers so they have the highest concatenated value #
WHILE BOOL swapped := FALSE;
FOR i pos TO UPB result - 1 DO
STRING l := TOSTRING result[ i pos ];
STRING r := TOSTRING result[ i pos + 1 ];
IF TOINT ( l + r ) < TOINT ( r + l ) THEN
INT t = result[ i pos + 1 ];
result[ i pos + 1 ] := result[ i pos ];
result[ i pos ] := t;
swapped := TRUE
FI
OD;
swapped
DO SKIP OD;
result
FI # CONCATSORT # ;
# prints the array a #
OP PRINT = ( []INT a )VOID:
FOR a pos FROM LWB a TO UPB a DO
print( ( TOSTRING a[ a pos ] ) )
OD # PRINT # ;
# task test cases #
PRINT CONCATSORT []INT( 1, 34, 3, 98, 9, 76, 45, 4 );
print( ( newline ) );
PRINT CONCATSORT []INT( 54, 546, 548, 60 );
print( ( newline ) )
END
- Output:
998764543431 6054854654
Arturo
largestConcInt: function [arr]->
max map permutate arr 's [
to :integer join map s => [to :string]
]
loop [[1 34 3 98 9 76 45 4] [54 546 548 60]] 'a ->
print largestConcInt a
AutoHotkey
LargestConcatenatedInts(var){
StringReplace, var, A_LoopField,%A_Space%,, all
Sort, var, D`, fConcSort
StringReplace, var, var, `,,, all
return var
}
ConcSort(a, b){
m := a . b , n := b . a
return m < n ? 1 : m > n ? -1 : 0
}
Examples:
d =
(
1, 34, 3, 98, 9, 76, 45, 4
54, 546, 548, 60
4 , 45, 54, 5
)
loop, parse, d, `n
MsgBox % LargestConcatenatedInts(A_LoopField)
- Output:
998764543431 6054854654 554454
AWK
function cmp(i1, v1, i2, v2, u1, u2) {
u1 = v1""v2;
u2 = v2""v1;
return (u2 - u1)
}
function largest_int_from_concatenated_ints(X) {
PROCINFO["sorted_in"]="cmp";
u="";
for (i in X) u=u""X[i];
return u
}
BEGIN {
split("1 34 3 98 9 76 45 4",X);
print largest_int_from_concatenated_ints(X)
split("54 546 548 60",X);
print largest_int_from_concatenated_ints(X)
}
- Output:
998764543431 6054854654
BBC BASIC
DIM Nums%(10)
Nums%()=1,34,3,98,9,76,45,4
PRINT FNlargestint(8)
Nums%()=54,546,548,60
PRINT FNlargestint(4)
END
DEF FNlargestint(len%)
LOCAL i%,l$,a$,b$,sorted%
REPEAT
sorted%=TRUE
FOR i%=0 TO len%-2
a$=STR$Nums%(i%)
b$=STR$Nums%(i%+1)
IF a$+b$<b$+a$ SWAP Nums%(i%),Nums%(i%+1):sorted%=FALSE
NEXT
UNTIL sorted%
FOR i%=0 TO len%-1
l$+=STR$Nums%(i%)
NEXT
=l$
- Output:
998764543431 6054854654
Bracmat
( ( maxnum
= A Z F C
. !arg:#
| !arg
: %@?F
?
( #%@?C
& ( str$(!F !C)+-1*str$(!C !F):~<0
| !C:?F
)
& ~
)
?
| !arg:?A !F ?Z&!F maxnum$(!A !Z)
)
& out$(str$(maxnum$(1 34 3 98 9 76 45 4)))
& out$(str$(maxnum$(54 546 548 60)))
);
- Output:
998764543431 6054854654
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int catcmp(const void *a, const void *b)
{
char ab[32], ba[32];
sprintf(ab, "%d%d", *(int*)a, *(int*)b);
sprintf(ba, "%d%d", *(int*)b, *(int*)a);
return strcmp(ba, ab);
}
void maxcat(int *a, int len)
{
int i;
qsort(a, len, sizeof(int), catcmp);
for (i = 0; i < len; i++)
printf("%d", a[i]);
putchar('\n');
}
int main(void)
{
int x[] = {1, 34, 3, 98, 9, 76, 45, 4};
int y[] = {54, 546, 548, 60};
maxcat(x, sizeof(x)/sizeof(x[0]));
maxcat(y, sizeof(y)/sizeof(y[0]));
return 0;
}
- Output:
998764543431 6054854654
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
var source1 = new int[] { 1, 34, 3, 98, 9, 76, 45, 4 };
var source2 = new int[] { 54, 546, 548, 60 };
var largest1 = LargestPossibleSequence(source1);
var largest2 = LargestPossibleSequence(source2);
Console.WriteLine("The largest possible integer from set 1 is: {0}", largest1);
Console.WriteLine("The largest possible integer from set 2 is: {0}", largest2);
}
static long LargestPossibleSequence(int[] ints)
{
return long.Parse(string.Join("", ints.OrderBy(i => i, new IntConcatenationComparer()).Reverse()));
}
}
class IntConcatenationComparer : IComparer<int>
{
public int Compare(int x, int y)
{
var xy = int.Parse(x.ToString() + y.ToString());
var yx = int.Parse(y.ToString() + x.ToString());
return xy - yx;
}
}
- Output:
The largest possible integer from set 1 is: 998764543431 The largest possible integer from set 2 is: 6054854654
C++
#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <string>
std::string findLargestConcat ( std::vector< int > & mynumbers ) {
std::vector<std::string> concatnumbers ;
std::sort ( mynumbers.begin( ) , mynumbers.end( ) ) ;
do {
std::ostringstream numberstream ;
for ( int i : mynumbers )
numberstream << i ;
concatnumbers.push_back( numberstream.str( ) ) ;
} while ( std::next_permutation( mynumbers.begin( ) ,
mynumbers.end( ) )) ;
return *( std::max_element( concatnumbers.begin( ) ,
concatnumbers.end( ) ) ) ;
}
int main( ) {
std::vector<int> mynumbers = { 98, 76 , 45 , 34, 9 , 4 , 3 , 1 } ;
std::vector<int> othernumbers = { 54 , 546 , 548 , 60 } ;
std::cout << "The largest concatenated int is " <<
findLargestConcat( mynumbers ) << " !\n" ;
std::cout << "And here it is " << findLargestConcat( othernumbers )
<< " !\n" ;
return 0 ;
}
- Output:
The largest concatenated int is 998764543431 ! And here it is 6054854654 !
Ceylon
shared void run() {
function comparator(Integer x, Integer y) {
assert (is Integer xy = Integer.parse(x.string + y.string),
is Integer yx = Integer.parse(y.string + x.string));
return yx <=> xy;
}
function biggestConcatenation({Integer*} ints) => "".join(ints.sort(comparator));
value test1 = {1, 34, 3, 98, 9, 76, 45, 4};
print(biggestConcatenation(test1));
value test2 = {54, 546, 548, 60};
print(biggestConcatenation(test2));
}
Clojure
(defn maxcat [coll]
(read-string
(apply str
(sort (fn [x y]
(apply compare
(map read-string [(str y x) (str x y)])))
coll))))
(prn (map maxcat [[1 34 3 98 9 76 45 4] [54 546 548 60]]))
- Output:
(998764543431 6054854654)
Common Lisp
Sort by two-by-two comparison of largest concatenated result
(defun int-concat (ints)
(read-from-string (format nil "~{~a~}" ints)))
(defun by-biggest-result (first second)
(> (int-concat (list first second)) (int-concat (list second first))))
(defun make-largest-int (ints)
(int-concat (sort ints #'by-biggest-result)))
- Output:
> (make-largest-int '(1 34 3 98 9 76 45 4)) 998764543431 > (make-largest-int '(54 546 548 60)) 6054854654
Variation around the sort with padded most significant digit
;; Sort criteria is by most significant digit with least digits used as a tie
;; breaker
(defun largest-msd-with-less-digits (x y)
(flet ((first-digit (x)
(digit-char-p (aref x 0))))
(cond ((> (first-digit x)
(first-digit y))
t)
((> (first-digit y)
(first-digit x))
nil)
((and (= (first-digit x)
(first-digit y))
(> (length x)
(length y)))
nil)
(t t))))
(loop
:for input :in '((54 546 548 60) (1 34 3 98 9 76 45 4))
:do (format t "~{~A~}~%"
(sort (mapcar #'write-to-string input)
#'largest-msd-with-less-digits)))
- Output:
6054548546 998764453341
D
The three algorithms. Uses the second module from the Permutations Task.
import std.stdio, std.algorithm, std.conv, std.array, permutations2;
auto maxCat1(in int[] arr) pure @safe {
return arr.to!(string[]).permutations.map!join.reduce!max;
}
auto maxCat2(in int[] arr) pure nothrow @safe {
return arr.to!(string[]).sort!q{b ~ a < a ~ b}.join;
}
auto maxCat3(in int[] arr) /*pure nothrow @safe*/ {
immutable maxL = arr.reduce!max.text.length;
return arr.to!(string[])
.schwartzSort!(s => s.replicate(maxL/s.length + 1), "a > b")
.join;
}
void main() {
const lists = [[1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60]];
[&maxCat1, &maxCat2, &maxCat3].map!(cat => lists.map!cat).writeln;
}
- Output:
[["998764543431", "6054854654"], ["998764543431", "6054854654"], ["998764543431", "6054854654"]]
Delphi
See Pascal.
EasyLang
func con a b .
t = 10
while b >= t
t *= 10
.
return a * t + b
.
func$ max a[] .
n = len a[]
for i to n - 1
for j = i + 1 to n
if con a[i] a[j] < con a[j] a[i]
swap a[i] a[j]
.
.
.
for v in a[]
r$ &= v
.
return r$
.
print max [ 1 34 3 98 9 76 45 4 ]
print max [ 54 546 548 60 ]
- Output:
998764543431 6054854654
Elixir
defmodule RC do
def largest_int(list) do
sorted = Enum.sort(list, fn x,y -> "#{x}#{y}" >= "#{y}#{x}" end)
Enum.join(sorted)
end
end
IO.inspect RC.largest_int [1, 34, 3, 98, 9, 76, 45, 4]
IO.inspect RC.largest_int [54, 546, 548, 60]
- Output:
"998764543431" "6054854654"
Erlang
-module( largest_int_from_concatenated ).
-export( [ints/1, task/0] ).
ints( Ints ) ->
Int_strings = [erlang:integer_to_list(X) || X <- Ints],
Pad_ints = [{X ++ X, X} || X <- Int_strings],
erlang:list_to_integer( lists:append([Int || {_Pad, Int} <- lists:reverse(lists:sort(Pad_ints))]) ).
task() ->
[io:fwrite("Largest ~p from ~p~n", [ints(X), X]) || X <- [[1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60]]].
- Output:
8> largest_int_from_concatenated:task(). Largest 998764543431 from [1,34,3,98,9,76,45,4] Largest 6054854654 from [54,546,548,60]
F#
// Form largest integer which is a permutation from a list of integers. Nigel Galloway: March 21st., 2018
let fN g = List.map (string) g |> List.sortWith(fun n g->if n+g<g+n then 1 else -1) |> System.String.Concat
- Output:
fN [1; 34; 3; 98; 9; 76; 45; 4] -> "998764543431" fN [54; 546; 548; 60] -> "6054854654"
Factor
Using algorithm 3:
USING: assocs io kernel math qw sequences sorting ;
IN: rosetta-code.largest-int
: pad ( target seq -- padded )
2dup length / swap <repetition> concat swap head ;
: largest-int ( seq -- )
dup dup [ length ] map supremum ! find longest length so we know how much to pad
[ swap pad ] curry map ! pad the integers
<enum> sort-values ! sort the padded integers
keys ! find the original indices of the sorted integers
swap nths ! order non-padded integers according to their sorted order
reverse concat print ;
qw{ 1 34 3 98 9 76 45 4 } qw{ 54 546 548 60 } [ largest-int ] bi@
- Output:
998764543431 6054854654
Or alternatively, a translation of F#.
USING: kernel math.order qw sequences sorting ;
: fn ( seq -- str )
[ 2dup swap [ append ] 2bi@ after? +lt+ +gt+ ? ] sort concat ;
- Output:
qw{ 1 34 3 98 9 76 45 4 } qw{ 54 546 548 60 } [ fn ] bi@ --- Data stack: "998764543431" "6054854654"
Fortran
There is often a potential ambiguity when reading numbers. While three definitely names the Platonic number notion, 3 might instead be regarded as being a text that happens to have the glyph of a number but is not a number. This sort of discussion arises when a spreadsheet has read in a text file and behold! numbers are on the display and they look just like what is displayed when numbers are being shown, but, they are not numbers, they are only drawn that way. Within the spreadsheet they are parts of some text, and the notion that takes over is one of a "blunt, heavy object", not alas close to hand.
So, the plan is to regard the numbers as being text sequences aligned to the left, containing only digit characters of course - except for the fact that CHARACTER variables often end up having trailing spaces. F2003 formalised a scheme whereby such variables can be "cut-to-fit" as execution proceeds but with earlier Fortrans the standard method is to pay attention to the number of characters in use. F90 introduced a function LEN_TRIM(text) to return the index of the last non-blank character in a text so the only problem now is to decide on how long might the largest number be (and by representing numbers as text strings, there is no difficulty with the limits of INTEGER*2 or INTEGER*4 etc.), and what will be the maximum number of numbers. By devising a subroutine to do the work, these issues can be handled by the caller that is providing the data. The subroutine however intends to sort the collection of texts. This could be done by damaging its parameter which might be regarded as impolite or even unwanted so instead the sort is effected via an array XLAT and juggling its values. This has the advantage that the possibly large elements of the text array are not being moved about, but means that the subroutine must be able to have an XLAT array that is "large enough". F90 standardised the ability for a routine to declare such an array at run-time; previously, arrays within a subroutine (or indeed anywhere) had to have a size fixed at compilation time. In the past this might have been handled by the caller supplying such an array as an additional parameter.
Passing arrays as parameters can be tricky, especially for multi-dimensional arrays. This uses the old style whereby the size is left unstated via the * in TEXT(*)
, though one could use TEXT(N)
instead - but at the risk that the actual value of N is wrong and array index checking might be confused thereby. Still earlier one would simply place some integer value there, any valid integer, as in TEXT(666)
, and not worry about bound checking at all because old-style compilers did not produce checking code even if it was wanted. F90 standardised the MODULE protocol, within which the size is specified as TEXT(:)
whereby secret additional parameters are supplied that contain the actual bound information and bound checking will be correct, possibly not so if the TEXT(N)
form is used instead and N is wrong. This extra overhead in every use is possibly better than undetected errors in some uses...
The sorting of the text array was to be by the notorious BubbleSort, taking advantage of the fact that each pass delivers the maximum value of the unsorted portion to its final position: the output could thereby be produced as the sort worked. Rather than mess about with early termination (no element being swapped) or attention to the bounds within which swapping took place, attention concentrated upon the comparison. Because of the left-alignment of the texts, a simple comparison seemed sufficient until I thought of unequal text lengths and then the following example. Suppose there are two numbers, 5, and one of 54, 55, or 56 as the other. Via normal comparisons, the 5 would always be first (because short texts are considered expanded with trailing spaces when compared against longer texts, and a space precedes every digit) however the biggest ordering is 5 54 for the first case but 56 5 for the last. This possibility is not exemplified in the specified trial sets. So, a more complex comparison is required. One could of course write a suitable function and consider the issue there but instead the comparison forms the compound text in the same manner as the result will be, in the two ways AB and BA, and looks to see which yields the bigger sequence. This need only be done for unequal length text pairs.
The source is F77 style, except for the declaration of XLAT(N), the use of <N> in the FORMAT statements instead of some large constant or similar, and the ability to declare an array via constants as in (/"5","54"/)
rather than mess about declaring arrays and initialising them separately. The I0
format code to convert a number (an actual number) into a digit string aligned leftwards in a CHARACTER variable of sufficient size is also a F90 introduction, though the B6700 compiler allowed a code J
instead. This last is to demonstrate usage of actual numbers for those unpersuaded by the argument for ambiguity that allows for texts. If the I0
format code is unavailable then I9
(or some suitable size) could be used, followed by text = ADJUSTL(text)
, except that this became an intrinsic function only in F90, so perhaps you will have to write a simple alignment routine.
SUBROUTINE SWAP(A,B) !Why can't the compiler supply these!
INTEGER A,B,T
T = B
B = A
A = T
END
SUBROUTINE BIGUP(TEXT,N) !Outputs the numbers in TEXT to give the biggest number.
CHARACTER*(*) TEXT(*) !The numbers as text, aligned left.
INTEGER N !The number of them.
INTEGER XLAT(N),L(N) !An index and a set of lengths.
INTEGER I,J,M !Assorted steppers.
INTEGER TI,TJ !Fingers to a text.
INTEGER LI,LJ !Lengths of the fingered texts.
INTEGER MSG !I/O unit number.
COMMON /IODEV/ MSG !Old style.
DO I = 1,N !Step through my supply of texts.
XLAT(I) = I !Preparing a finger to them.
L(I) = LEN_TRIM(TEXT(I)) !And noting their last non-blank.
END DO !On to the next.
WRITE (MSG,1) "Supplied",(TEXT(I)(1:L(I)), I = 1,N) !Show the grist.
1 FORMAT (A12,":",<N>(A,",")) !Instead of <N>, 666 might suffice.
Crude bubblesort. No attempt at noting the bounds of swaps made.
DO M = N,1,-1 !Just for fun, go backwards.
DO I = 2,M !Start a scan.
J = I - 1 !Comparing element I to element I - 1.
TI = XLAT(I) !Thus finger the I'th text in XLAT order.
TJ = XLAT(J) !And its supposed predecessor.
LI = L(TI) !The length of the fingered text.
LJ = L(TJ) !All this to save on typing below.
IF (LI .EQ. LJ) THEN !If the texts are equal lengths,
IF (TEXT(TI).LT.TEXT(TJ)) CALL SWAP(XLAT(I),XLAT(J)) !A simple comparison.
ELSE !But if not, construct the actual candidate texts for comparison.
IF (TEXT(TI)(1:LI)//TEXT(TJ)(1:LJ) !These two will be the same length.
1 .LT.TEXT(TJ)(1:LJ)//TEXT(TI)(1:LI)) !Just as above.
2 CALL SWAP(XLAT(I),XLAT(J)) !J shall now follow I.
END IF !So much for that comparison.
END DO !On to the next.
END DO !The original plan was to reveal element XLAT(M) as found.
WRITE (MSG,2) "Biggest",(TEXT(XLAT(I))(1:L(XLAT(I))),I = N,1,-1) !But, all at once is good too.
2 FORMAT (A12,":",<N>(A," ")) !The space maintains identity.
END !That was fun.
PROGRAM POKE
CHARACTER*4 T1(10) !Prepare some example arrays.
CHARACTER*4 T2(4) !To hold the specified examples.
INTEGER MSG
COMMON /IODEV/ MSG
DATA T1(1:8)/"1","34","3","98","9","76","45","4"/
DATA T2/"54","546","548","60"/
MSG = 6 !Standard output.
WRITE (MSG,1)
1 FORMAT ("Takes a list of integers and concatenates them so as ",
1 "to produce the biggest possible number.",/,
2 "The result is shown with spaces between the parts ",
3 "to show provenance. Ignore them otherwise."/)
CALL BIGUP(T1,8)
WRITE (MSG,*)
CALL BIGUP(T2,4)
WRITE (MSG,*) "These are supplied in lexicographical order..."
CALL BIGUP((/"5","54"/),2)
WRITE (MSG,*) "But this is not necessarily the biggest order."
CALL BIGUP((/"5","56"/),2)
WRITE (MSG,*) "And for those who count..."
DO I = 1,10
WRITE (T1(I),"(I0)") I !This format code produces only the necessary text.
END DO !Thus, the numbers are aligned left in the text field.
CALL BIGUP(T1,10)
END
Output: the Fortran compiler ignores spaces when reading fortran source, so, hard-core fortranners should have no difficulty doing likewise for the output...
Takes a list of integers and concatenates them so as to produce the biggest possible number. The result is shown with spaces between the parts to show provenance. Ignore them otherwise. Supplied:1,34,3,98,9,76,45,4, Biggest:9 98 76 45 4 34 3 1 Supplied:54,546,548,60, Biggest:60 548 546 54 These are supplied in lexicographical order... Supplied:5,54, Biggest:5 54 But this is not necessarily the biggest order. Supplied:5,56, Biggest:56 5 And for those who count... Supplied:1,2,3,4,5,6,7,8,9,10, Biggest:9 8 7 6 5 4 3 2 1 10
FreeBASIC
#define MAXDIGITS 8
function catint( a as string, b as string ) as uinteger
return valint(a+b)
end function
function grt( a as string, b as string ) as boolean
return catint(a, b)>catint(b, a)
end function
sub shellsort( a() as string )
'quick and dirty shellsort, not the focus of this exercise
dim as uinteger gap = ubound(a), i, j, n=ubound(a)
dim as string temp
do
gap = int(gap / 2.2)
for i=gap to n
temp = a(i)
j=i
while j>=gap andalso grt( a(j-gap), temp )
a(j) = a(j - gap)
j -= gap
wend
a(j) = temp
next i
loop until gap = 1
end sub
sub sort_and_print( a() as string )
dim as uinteger i
dim as string outstring = ""
shellsort(a())
for i=0 to ubound(a)
outstring = a(i)+outstring
next i
print outstring
end sub
dim as string set1(8) = {"1", "34", "3", "98", "9", "76", "45", "4"}
dim as string set2(4) = {"54", "546", "548", "60"}
sort_and_print(set1())
sort_and_print(set2())
- Output:
998764543431 6054854654
Frink
a = [[1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60]]
f = {|p| parseInt[join["",p]] }
for s = a
println[max[map[f, s.lexicographicPermute[]]]]
- Output:
998764543431 6054854654
Gambas
Click this link to run this code
'Largest int from concatenated ints
Public Sub Main()
Dim iList1 As Integer[] = [1, 34, 3, 98, 9, 76, 45, 4] 'Integer list 1
Dim iList2 As Integer[] = [54, 546, 548, 60] 'Integer list 2
Calc(iList1) 'Send List 1 to Calc routine
Calc(iList2) 'Send List 2 to Calc routine
End
'_________________________________________________________________________________________
Public Sub Calc(iList As Integer[])
Dim siCount1, siCount2, siCounter As Short 'Counters
Dim sList As New String[] 'To hold converted integers
Dim bTrigger As Boolean 'To trigger a found match
For Each siCount1 In iList 'For each integer in the list..
sList.Add(Str(siCount1)) 'Convert to a string and add to sList
If Len(Str(siCount1)) > siCounter Then 'If the length of the string is greater than siCounter then..
siCounter = Len(Str(siCount1)) 'siCounter = length of the string
End If
Next
For siCount1 = 0 To sList.Max 'For each item in sList
If Len(sList[siCount1]) < siCounter Then 'If the length of the string is less that siCounter then..
sList[siCount1] &= Right(sList[siCount1], 1) 'Add the same digit to the string e.g. in list 1 "9" becomes "99", list 2 "54" becomes "544"
End If
Next
sList.Sort(gb.Descent) 'Sort the list in decending order
For siCount1 = 0 To sList.Max 'For each item in sList
bTrigger = False 'Set bTrigger to False
For siCount2 = 0 To iList.Max 'Loop through each item in iList
If Val(sList[siCount1]) = iList[siCount2] Then 'If the value of each is the same e.g. "98" = 98 then
bTrigger = True 'Set bTrigger to True
Continue 'Exit the loop
Endif
Next
If Not bTrigger Then 'If there was no match e.g. there is no "99" then..
sList[siCount1] = Left(sList[siCount1], siCounter - 1) 'Strip out the end digit e.g. "99" becomes 9 again
End If
Next
Print Val(sList.Join("")) 'Join all items in sList together and print
End
Output:
998764543431 6054854654
Go
// Variation of method 3. Repeat digits to at least the size of the longest,
// then sort as strings.
package main
import (
"fmt"
"math/big"
"sort"
"strconv"
"strings"
)
type c struct {
i int
s, rs string
}
type cc []*c
func (c cc) Len() int { return len(c) }
func (c cc) Less(i, j int) bool { return c[j].rs < c[i].rs }
func (c cc) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
// Function required by task. Takes a list of integers, returns big int.
func li(is ...int) *big.Int {
ps := make(cc, len(is))
ss := make([]c, len(is))
ml := 0
for j, i := range is {
p := &ss[j]
ps[j] = p
p.i = i
p.s = strconv.Itoa(i)
if len(p.s) > ml {
ml = len(p.s)
}
}
for _, p := range ps {
p.rs = strings.Repeat(p.s, (ml+len(p.s)-1)/len(p.s))
}
sort.Sort(ps)
s := make([]string, len(ps))
for i, p := range ps {
s[i] = p.s
}
b, _ := new(big.Int).SetString(strings.Join(s, ""), 10)
return b
}
func main() {
fmt.Println(li(1, 34, 3, 98, 9, 76, 45, 4))
fmt.Println(li(54, 546, 548, 60))
}
- Output:
998764543431 6054854654
Groovy
def largestInt = { c -> c.sort { v2, v1 -> "$v1$v2" <=> "$v2$v1" }.join('') as BigInteger }
Testing:
assert largestInt([1, 34, 3, 98, 9, 76, 45, 4]) == 998764543431
assert largestInt([54, 546, 548, 60]) == 6054854654
Haskell
Compare repeated string method
import Data.List (sortBy)
import Data.Ord (comparing)
main = print (map maxcat [[1,34,3,98,9,76,45,4], [54,546,548,60]] :: [Integer])
where
sorted xs = let pad x = concat $ replicate (maxLen `div` length x + 1) x
maxLen = maximum $ map length xs
in sortBy (flip $ comparing pad) xs
maxcat = read . concat . sorted . map show
- Output:
[998764543431,6054854654]
Since repeating numerical string "1234" is the same as taking all the digits of 1234/9999 after the decimal point, the following does essentially the same as above:
import Data.List (sortBy)
import Data.Ord (comparing)
import Data.Ratio ((%))
nines = iterate ((+9).(*10)) 9
maxcat = foldl (\a (n,d)->a * (1 + d) + n) 0 .
sortBy (flip $ comparing $ uncurry (%)) .
map (\a->(a, head $ dropWhile (<a) nines))
main = mapM_ (print.maxcat) [[1,34,3,98,9,76,45,4], [54,546,548,60]]
Sort on comparison of concatenated ints method
import Data.List (sortBy)
main = print (map maxcat [[1,34,3,98,9,76,45,4], [54,546,548,60]] :: [Integer])
where sorted = sortBy (\a b -> compare (b++a) (a++b))
maxcat = read . concat . sorted . map show
- Output as above.
Try all permutations method
import Data.List (permutations)
main :: IO ()
main =
print
(maxcat <$> [[1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60]] :: [Integer])
where
maxcat = read . maximum . fmap (concatMap show) . permutations
- Output as above.
Icon and Unicon
This solution only works in Unicon as it uses a Heap class to do the heavy lifting.
import Collections # For the Heap (dense priority queue) class
procedure main(a)
write(lici(a))
end
procedure lici(a)
every (result := "") ||:= Heap(a,,cmp).gen()
return result
end
procedure cmp(a,b)
return (a||b) > (b||a)
end
Sample runs:
->lici 1 34 3 98 9 76 45 4 998764543431 ->lici 54 546 548 60 6054854654 ->
J
Here we use the "pad the integers to the same size by repeating the digits then sort using these repeated integers as a sort key" approach: Solution:
maxlen=: [: >./ #&>
maxnum=: (0 ". ;)@(\: maxlen $&> ])@(8!:0)
Usage:
maxnum&> 1 34 3 98 9 76 45 4 ; 54 546 548 60
998764543431 6054854654
Java
This example sets up a comparator to order the numbers using Collections.sort
as described in method #3 (padding and reverse sorting).
It was also necessary to make a join method to meet the output requirements.
import java.util.*;
public class IntConcat {
private static Comparator<Integer> sorter = new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2){
String o1s = o1.toString();
String o2s = o2.toString();
if(o1s.length() == o2s.length()){
return o2s.compareTo(o1s);
}
int mlen = Math.max(o1s.length(), o2s.length());
while(o1s.length() < mlen * 2) o1s += o1s;
while(o2s.length() < mlen * 2) o2s += o2s;
return o2s.compareTo(o1s);
}
};
public static String join(List<?> things){
String output = "";
for(Object obj:things){
output += obj;
}
return output;
}
public static void main(String[] args){
List<Integer> ints1 = new ArrayList<Integer>(Arrays.asList(1, 34, 3, 98, 9, 76, 45, 4));
Collections.sort(ints1, sorter);
System.out.println(join(ints1));
List<Integer> ints2 = new ArrayList<Integer>(Arrays.asList(54, 546, 548, 60));
Collections.sort(ints2, sorter);
System.out.println(join(ints2));
}
}
import java.util.Comparator;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public interface IntConcat {
public static Comparator<Integer> SORTER = (o1, o2) -> {
String o1s = o1.toString();
String o2s = o2.toString();
if (o1s.length() == o2s.length()) {
return o2s.compareTo(o1s);
}
int mlen = Math.max(o1s.length(), o2s.length());
while (o1s.length() < mlen * 2) {
o1s += o1s;
}
while (o2s.length() < mlen * 2) {
o2s += o2s;
}
return o2s.compareTo(o1s);
};
public static void main(String[] args) {
Stream<Integer> ints1 = Stream.of(
1, 34, 3, 98, 9, 76, 45, 4
);
System.out.println(ints1
.parallel()
.sorted(SORTER)
.map(String::valueOf)
.collect(Collectors.joining())
);
Stream<Integer> ints2 = Stream.of(
54, 546, 548, 60
);
System.out.println(ints2
.parallel()
.sorted(SORTER)
.map(String::valueOf)
.collect(Collectors.joining())
);
}
}
- Output:
998764543431 6054854654
JavaScript
ES5
(function () {
'use strict';
// maxCombine :: [Int] -> Int
function maxCombine(xs) {
return parseInt(
xs.sort(
function (x, y) {
var a = x.toString(),
b = y.toString(),
ab = parseInt(a + b),
ba = parseInt(b + a);
return ab > ba ? -1 : (ab < ba ? 1 : 0);
}
)
.join(''), 10
);
}
return [
[1, 34, 3, 98, 9, 76, 45, 4],
[54, 546, 548, 60]
].map(maxCombine);
})();
- Output:
[998764543431, 6054854654]
ES6
var maxCombine = (a) => +(a.sort((x, y) => +("" + y + x) - +("" + x + y)).join(''));
// test & output
console.log([
[1, 34, 3, 98, 9, 76, 45, 4],
[54, 546, 548, 60]
].map(maxCombine));
jq
Padding
For jq versions greater than 1.4, it may be necessary to change "sort_by" to "sort".
def largest_int:
def pad(n): . + (n - length) * .[length-1:];
map(tostring)
| (map(length) | max) as $max
| map([., pad($max)])
| sort_by( .[1] )
| map( .[0] ) | reverse | join("") ;
# Examples:
([1, 34, 3, 98, 9, 76, 45, 4],
[54, 546, 548, 60]) | largest_int
- Output:
$ /usr/local/bin/jq -n -M -r -f Largest_int_from_concatenated_ints.jq 998764543431 6054854654
Custom Sort
The following uses quicksort/1:
def largest_int:
map(tostring)
| quicksort( .[0] + .[1] < .[1] + .[0] )
| reverse | join("") ;
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.
function maxconcat(arr::Vector{<:Integer})
b = sort(string.(arr); lt=(x, y) -> x * y < y * x, rev=true) |> join
return try parse(Int, b) catch parse(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 arr in tests
println("Max concatenating in $arr:\n -> ", maxconcat(arr))
end
- Output:
Max concatenating in [1, 34, 3, 98, 9, 76, 45, 4]: -> 998764543431 Max concatenating in [54, 546, 548, 60]: -> 6054854654 Max concatenating in [1, 34, 3, 98, 9, 76, 45, 4, 54, 546, 548, 60]: -> 9987660548546544543431
Kotlin
import kotlin.Comparator
fun main(args: Array<String>) {
val comparator = Comparator<Int> { x, y -> "$x$y".compareTo("$y$x") }
fun findLargestSequence(array: IntArray): String {
return array.sortedWith(comparator.reversed()).joinToString("") { it.toString() }
}
for (array in listOf(
intArrayOf(1, 34, 3, 98, 9, 76, 45, 4),
intArrayOf(54, 546, 548, 60),
)) {
println("%s -> %s".format(array.contentToString(), findLargestSequence(array)))
}
}
- Output:
[1, 34, 3, 98, 9, 76, 45, 4] -> 998764543431 [54, 546, 548, 60] -> 6054854654
Lua
function icsort(numbers)
table.sort(numbers,function(x,y) return (x..y) > (y..x) end)
return numbers
end
for _,numbers in pairs({{1, 34, 3, 98, 9, 76, 45, 4}, {54, 546, 548, 60}}) do
print(('Numbers: {%s}\n Largest integer: %s'):format(
table.concat(numbers,","),table.concat(icsort(numbers))
))
end
- Output:
Numbers: {1,34,3,98,9,76,45,4} Largest integer: 998764543431 Numbers: {54,546,548,60} Largest integer: 6054854654
Mathematica /Wolfram Language
makeLargestInt[list_] := Module[{sortedlist},
sortedlist = Sort[list, Order[ToString[#1] <> ToString[#2], ToString[#2] <> ToString[#1]] < 0 &];
Map[ToString, sortedlist] // StringJoin // FromDigits
]
(* testing with two examples *)
makeLargestInt[{1, 34, 3, 98, 9, 76, 45, 4}]
makeLargestInt[{54, 546, 548, 60}]
- Output:
998764543431 6054854654
Maxima
/* Function that decompose a number into a list of its digits using conversions between numbers and strings */
decompose_n_s(n):=block(
string(n),
charlist(%%),
map(eval_string,%%))$
/* Function that orders the list obtained by decompose_n_ according to ordergreat and then orders the result to reached what is needed to solve the problem */
largest_from_list(lst):=(
sort(map(decompose_n_s,lst),ordergreatp),
sort(%%,lambda([a,b],if last(a)>last(b) then rest(b,-1)=a else rest(a,-1)=b)),
map(string,flatten(%%)),
simplode(%%),
eval_string(%%));
/* Test cases */
test1: [1, 34, 3, 98, 9, 76, 45, 4]$
test2: [54, 546, 548, 60]$
largest_from_list(test1);
largest_from_list(test2);
- Output:
998764543431 6054854654
min
(quote cons "" join) :s+
('string map (over over swap s+ 's+ dip <) sort "" join int) :fn
(1 34 3 98 9 76 45 4) fn puts!
(54 546 548 60) fn puts!
- Output:
998764543431 6054854654
NetRexx
/* NetRexx */
options replace format comments java crossref symbols nobinary
runSample(arg)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method largestInt(il) public static
ri = ''
wa = ''
-- put the list into an indexed string
wa[0] = il.words
loop ww = 1 to wa[0]
wa[ww] = il.word(ww)
end ww
-- order the list
loop wx = 1 to wa[0] - 1
loop wy = wx + 1 to wa[0]
xx = wa[wx]
yy = wa[wy]
xy = xx || yy
yx = yy || xx
if xy < yx then do
-- swap xx and yy
wa[wx] = yy
wa[wy] = xx
end
end wy
end wx
-- rebuild list from indexed string
loop ww = 1 to wa[0]
ri = ri wa[ww]
end ww
return ri.space(0) -- concatenate the list elements into a single numeric
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) private static
ints = [ -
'1 34 3 98 9 76 45 4', -
'54 546 548 60' -
]
loop il over ints
say largestInt(il).right(20) ':' il.space(1, ',')
end il
return
- Output:
998764543431 : 1,34,3,98,9,76,45,4 6054854654 : 54,546,548,60
Nim
import algorithm, sequtils, strutils, sugar
proc maxNum(x: seq[int]): string =
var c = x.mapIt($it)
c.sort((x, y) => cmp(y&x, x&y))
c.join()
echo maxNum(@[1, 34, 3, 98, 9, 76, 45, 4])
echo maxNum(@[54, 546, 548, 60])
- Output:
998764543431 6054854654
OCaml
let myCompare a b = compare (b ^ a) (a ^ b)
let icsort nums = String.concat "" (List.sort myCompare (List.map string_of_int nums))
- testing
# icsort [1;34;3;98;9;76;45;4];; - : string = "998764543431" # icsort [54;546;548;60];; - : string = "6054854654"
Oforth
: largestInt map(#asString) sortWith(#[ 2dup + -rot swap + > ]) sum asInteger ;
- Output:
[ [1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60] ] map(#largestInt) . [998764543431, 6054854654]
PARI/GP
Sorts then joins. Most of the noise comes from converting a vector of integers into a concatenated integer: eval(concat(apply(n->Str(n),v)))
. Note that the short form eval(concat(apply(Str,v)))
is not valid here because Str
is variadic.
large(v)=eval(concat(apply(n->Str(n),vecsort(v,(x,y)->eval(Str(y,x,"-",x,y))))));
large([1, 34, 3, 98, 9, 76, 45, 4])
large([54, 546, 548, 60])
- Output:
%1 = 998764543431 %2 = 6054854654
Pascal
tested with freepascal.Used a more extreme example 3.
algorithm 3
const
base = 10;
MaxDigitCnt = 11;
source1 : array[0..7] of integer = (1, 34, 3, 98, 9, 76, 45, 4);
source2 : array[0..3] of integer = (54,546,548,60);
source3 : array[0..3] of integer = (60, 54,545454546,0);
type
tdata = record
datOrg,
datMod : LongWord;
datStrOrg : string[MaxDigitCnt];
end;
tArrData = array of tData;
procedure DigitCount(var n: tdata);
begin
with n do
//InttoStr is very fast
str(datOrg,datStrOrg);
end;
procedure InsertData(var n: tdata;data:LongWord);
begin
n.datOrg := data;
DigitCount(n);
end;
function FindMaxLen(const ArrData:tArrData): LongWord;
var
cnt : longInt;
res,t : LongWord;
begin
res := 0;// 1 is minimum
for cnt := High(ArrData) downto Low(ArrData) do
begin
t := length(ArrData[cnt].datStrOrg);
IF res < t then
res := t;
end;
FindMaxLen := res;
end;
procedure ExtendCount(var ArrData:tArrData;newLen: integer);
var
cnt,
i,k : integer;
begin
For cnt := High(ArrData) downto Low(ArrData) do
with ArrData[cnt] do
begin
datMod := datOrg;
i := newlen-length(datStrOrg);
k := 1;
while i > 0 do
begin
datMod := datMod *Base+Ord(datStrOrg[k])-Ord('0');
inc(k);
IF k >length(datStrOrg) then
k := 1;
dec(i);
end;
end;
end;
procedure SortArrData(var ArrData:tArrData);
var
i,
j,idx : integer;
tmpData : tData;
begin
For i := High(ArrData) downto Low(ArrData)+1 do
begin
idx := i;
j := i-1;
For j := j downto Low(ArrData) do
IF ArrData[idx].datMod < ArrData[j].datMod then
idx := j;
IF idx <> i then
begin
tmpData := ArrData[idx];
ArrData[idx]:= ArrData[i];
ArrData[i] := tmpData;
end;
end;
end;
procedure ArrDataOutput(const ArrData:tArrData);
var
i,l : integer;
s : AnsiString;
begin
{ the easy way
For i := High(ArrData) downto Low(ArrData) do
write(ArrData[i].datStrOrg);
writeln;
*}
l := 0;
For i := High(ArrData) downto Low(ArrData) do
inc(l,length(ArrData[i].datStrOrg));
setlength(s,l);
l:= 1;
For i := High(ArrData) downto Low(ArrData) do
with ArrData[i] do
begin
move(datStrOrg[1],s[l],length(datStrOrg));
inc(l,length(datStrOrg));
end;
writeln(s);
end;
procedure HighestInt(var ArrData:tArrData);
begin
ExtendCount(ArrData,FindMaxLen(ArrData));
SortArrData(ArrData);
ArrDataOutput(ArrData);
end;
var
i : integer;
tmpData : tArrData;
begin
// Source1
setlength(tmpData,length(source1));
For i := low(tmpData) to high(tmpData) do
InsertData(tmpData[i],source1[i]);
HighestInt(tmpData);
// Source2
setlength(tmpData,length(source2));
For i := low(tmpData) to high(tmpData) do
InsertData(tmpData[i],source2[i]);
HighestInt(tmpData);
// Source3
setlength(tmpData,length(source3));
For i := low(tmpData) to high(tmpData) do
InsertData(tmpData[i],source3[i]);
HighestInt(tmpData);
end.
- Output:
998764543431 6054854654 60545454546540
Inspired by Haskell
generate the repetition by dividing /(10^CountDigits-1) http://rosettacode.org/wiki/Largest_int_from_concatenated_ints#Compare_repeated_string_method
const
base = 10;
MaxDigitCnt = 11;
source1 : array[0..7] of LongInt = (10 , 34, 3, 98, 9, 76, 45, 4);
source2 : array[0..3] of LongInt = (54,546,548,60);
source3 : array[0..3] of LongInt = (0,2121212122,21,60);
type
tdata = record
datMod : double;
datOrg : LongInt;
//InttoStr is very fast and the string is always needed
datStrOrg : string[MaxDigitCnt];
end;
tArrData = array of tData;
procedure InsertData(var n: tdata;data:LongWord);
begin
with n do
begin
datOrg := data;
str(datOrg,datStrOrg);
end;
end;
function FindMaxLen(const ArrData:tArrData): LongWord;
var
cnt : longInt;
res,t : LongWord;
begin
res := 0;// 1 is minimum
for cnt := High(ArrData) downto Low(ArrData) do
begin
t := length(ArrData[cnt].datStrOrg);
IF res < t then
res := t;
end;
FindMaxLen := res;
end;
procedure ExtendData(var ArrData:tArrData;newLen: integer);
var
cnt,
i : integer;
begin
For cnt := High(ArrData) downto Low(ArrData) do
with ArrData[cnt] do
begin
//generating 10^length(datStrOrg)
datMod := 1;
i := length(datStrOrg);
// i always >= 1
repeat
datMod := base*datMod;
dec(i);
until i <= 0;
// 1/(datMod-1.0) = 1/(9...9)
datMod := datOrg/(datMod-1.0)+datOrg;
i := newlen-length(datStrOrg);
For i := i downto 1 do
datMod := datMod*Base;
end;
end;
procedure SortArrData(var ArrData:tArrData);
//selection sort
var
i,
j,idx : integer;
tmpData : tData;
begin
For i := High(ArrData) downto Low(ArrData)+1 do
begin
idx := i;
j := i-1;
//select max
For j := j downto Low(ArrData) do
IF ArrData[idx].datMod < ArrData[j].datMod then
idx := j;
//finally swap
IF idx <> i then
begin
tmpData := ArrData[idx];
ArrData[idx]:= ArrData[i];
ArrData[i] := tmpData;
end;
end;
end;
procedure ArrDataOutput(const ArrData:tArrData);
var
i : integer;
begin
{ the easy way}
For i := High(ArrData) downto Low(ArrData) do
write(ArrData[i].datStrOrg);
writeln;
end;
procedure HighestInt(var ArrData:tArrData);
begin
ExtendData(ArrData,FindMaxLen(ArrData));
SortArrData(ArrData);
ArrDataOutput(ArrData);
end;
var
i : integer;
tmpData : tArrData;
begin
// Source1
setlength(tmpData,length(source1));
For i := low(tmpData) to high(tmpData) do
InsertData(tmpData[i],source1[i]);
HighestInt(tmpData);
// Source2
setlength(tmpData,length(source2));
For i := low(tmpData) to high(tmpData) do
InsertData(tmpData[i],source2[i]);
HighestInt(tmpData);
// Source3
setlength(tmpData,length(source3));
For i := low(tmpData) to high(tmpData) do
InsertData(tmpData[i],source3[i]);
HighestInt(tmpData);
end.
- Output:
9987645434310 6054854654 602121212122210>
Perl
sub maxnum {
join '', sort { "$b$a" cmp "$a$b" } @_
}
print maxnum(1, 34, 3, 98, 9, 76, 45, 4), "\n";
print maxnum(54, 546, 548, 60), "\n";
- Output:
998764543431 6054854654
Phix
with javascript_semantics function catcmp(string a, string b) return compare(b&a,a&b) end function function method2(sequence s) return join(custom_sort(catcmp,apply(s,sprint)),"") end function ?method2({1,34,3,98,9,76,45,4}) ?method2({54,546,548,60})
- Output:
"998764543431" "6054854654"
PHP
function maxnum($nums) {
usort($nums, function ($x, $y) { return strcmp("$y$x", "$x$y"); });
return implode('', $nums);
}
echo maxnum(array(1, 34, 3, 98, 9, 76, 45, 4)), "\n";
echo maxnum(array(54, 546, 548, 60)), "\n";
- Output:
998764543431 6054854654
Picat
On the simpler cases, four methods are tested: 2 using permutations and 2 with different sorting methods.
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
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).
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().
Test
import util.
go =>
Ls = [[1, 34, 3, 98, 9, 76, 45, 4],
[54, 546, 548, 60],
[97, 9, 13, 979],
[9, 1, 95, 17, 5]
],
foreach(L in Ls)
test(L)
end,
nl.
% Test all implementations
test(L) =>
println(l=L),
maxof_inc(s_perm1(L,Num1), Num1),
println(s_perm1=Num1),
s_perm2(L,Num2),
println(s_perm2=Num2),
s_sort_conc(L,Num3),
println(s_sort_conc=Num3),
s_extend(L,Num4),
println(s_extent=Num4),
nl.
- Output:
l = [1,34,3,98,9,76,45,4] s_perm1 = 998764543431 s_perm2 = 998764543431 s_sort_conc = 998764543431 s_extend = 998764543431 l = [54,546,548,60] s_perm1 = 6054854654 s_perm2 = 6054854654 s_sort_conc = 6054854654 s_extend = 6054854654 l = [97,9,13,979] s_perm1 = 99799713 s_perm2 = 99799713 s_sort_conc = 99799713 s_extend = 99799713 l = [9,1,95,17,5] s_perm1 = 9955171 s_perm2 = 9955171 s_sort_conc = 9955171 s_extend = 9955171
Testing larger instance
Test a larger instance: 2000 random numbers between 1 and 100; about 5800 digits. The two permutation variants (s_perm1
and s_perm2
) takes too long on larger N, say N > 9.
go2 =>
garbage_collect(100_000_000),
_ = random2(),
N = 2000,
println(nums=N),
L = [random(1,1000) : _ in 1..N],
S = join([I.to_string : I in L],''),
println(str_len=S.len),
nl,
println("s_sort_conc:"),
time(s_sort_conc(L,_Num3)),
println("s_extend:"),
time(s_extend(L,_Num4)),
nl.
- Output:
nums = 2000 str_len = 5805 s_sort_conc: CPU time 0.54 seconds. s_extend: CPU time 0.526 seconds.
PicoLisp
Here are solutions for all three algorithms.
The third solution actually avoids padding the numbers, by converting them into circular lists and comparing these. As a drawback, however, this works only for unique lists (as the comparison of identical numbers would not terminate), so a better solution might involve additional checks.
(load "@lib/simul.l") # For 'permute'
Algorithm 1
(for L '((1 34 3 98 9 76 45 4) (54 546 548 60))
(prinl (maxi format (permute L))) )
Algorithm 2
(for L '((1 34 3 98 9 76 45 4) (54 546 548 60))
(prinl
(sort L
'((A B)
(>
(format (pack A B))
(format (pack B A)) ) ) ) ) )
Algorithm 3
(for L '((1 34 3 98 9 76 45 4) (54 546 548 60))
(prinl
(flip
(by '((N) (apply circ (chop N))) sort L) ) ) )
- Output:
in all three cases
998764543431 6054854654
PL/I
/* Largest catenation of integers 16 October 2013 */
/* Sort using method 2, comparing pairs of adjacent integers. */
Largest: procedure options (main);
declare s(*) char (20) varying controlled, n fixed binary;
get (n);
allocate s(n);
get list (s);
s = trim(s);
put skip edit (s) (a, x(1));
put skip list ('Largest integer=', Largest_integer());
largest_integer: procedure () returns (char(100) varying);
declare sorted bit (1);
declare (true value ('1'b), false value ('0'b)) bit (1);
declare i fixed binary;
declare temp character(20) varying;
do until (sorted);
sorted = true;
do i = 1 to n-1;
if char(s(i)) || char(s(i+1)) < char(s(i+1)) || char(s(i)) then
do;
temp = s(i); s(i) = s(i+1); s(i+1) = temp; sorted = false;
end;
end;
end;
return (string(s));
end largest_integer;
end Largest;
54 546 548 60 Largest integer= 6054854654 1 34 3 98 9 76 45 4 Largest integer= 998764543431
PowerShell
Using algorithm 3
Function Get-LargestConcatenation ( [int[]]$Integers )
{
# Get the length of the largest integer
$Length = ( $Integers | Sort -Descending | Select -First 1 ).ToString().Length
# Convert to an array of strings,
# sort by each number repeated Length times and truncated to Length,
# and concatenate (join)
$Concat = ( [string[]]$Integers | Sort { ( $_ * $Length ).Substring( 0, $Length ) } -Descending ) -join ''
# Convert to integer (upsizing type if needed)
try { $Integer = [ int32]$Concat }
catch { try { $Integer = [ int64]$Concat }
catch { $Integer = [bigint]$Concat } }
return $Integer
}
Get-LargestConcatenation 1, 34, 3, 98, 9, 76, 45, 4
Get-LargestConcatenation 54, 546, 548, 60
Get-LargestConcatenation 54, 546, 548, 60, 54, 546, 548, 60
- Output:
998764543431 6054854654 60605485485465465454
Prolog
Works with SWI-Prolog 6.5.3.
All permutations method
largest_int_v1(In, Out) :-
maplist(name, In, LC),
aggregate(max(V), get_int(LC, V), Out).
get_int(LC, V) :-
permutation(LC, P),
append(P, LV),
name(V, LV).
- Output:
?- largest_int_v1([1, 34, 3, 98, 9, 76, 45, 4], Out). Out = 998764543431. ?- largest_int_v1([54, 546, 548, 60], Out). Out = 6054854654.
Method 2
largest_int_v2(In, Out) :-
maplist(name, In, LC),
predsort(my_sort,LC, LCS),
append(LCS, LC1),
name(Out, LC1).
my_sort(R, L1, L2) :-
append(L1, L2, V1), name(I1, V1),
append(L2, L1, V2), name(I2, V2),
( I1 < I2, R = >; I1 = I2, R = '='; R = <).
% particular case 95 958
my_sort(>, [H1], [H1, H2 | _]) :-
H1 > H2.
my_sort(<, [H1], [H1, H2 | _]) :-
H1 < H2.
my_sort(R, [H1], [H1, H1 | T]) :-
my_sort(R, [H1], [H1 | T]).
% particular case 958 95
my_sort(>, [H1, H2 | _], [H1]) :-
H1 > H2.
my_sort(<, [H1, H2 | _], [H1]) :-
H1 < H2.
my_sort(R, [H1, H1 | T], [H1]) :-
my_sort(R, [H1 | T], [H1]) .
- Output:
?- largest_int_v2([1, 34, 3, 98, 9, 76, 45, 4], Out). Out = 998764543431 . ?- largest_int_v2([54, 546, 548, 60], Out). Out = 5486054654 .
Python
Python: Sort on comparison of concatenated ints method
This also shows one of the few times where cmp= is better than key= on sorted()
try:
cmp # Python 2 OK or NameError in Python 3
def maxnum(x):
return ''.join(sorted((str(n) for n in x),
cmp=lambda x,y:cmp(y+x, x+y)))
except NameError:
# Python 3
from functools import cmp_to_key
def cmp(x, y):
return -1 if x<y else ( 0 if x==y else 1)
def maxnum(x):
return ''.join(sorted((str(n) for n in x),
key=cmp_to_key(lambda x,y:cmp(y+x, x+y))))
for numbers in [(1, 34, 3, 98, 9, 76, 45, 4), (54, 546, 548, 60)]:
print('Numbers: %r\n Largest integer: %15s' % (numbers, maxnum(numbers)))
- Output:
Numbers: (1, 34, 3, 98, 9, 76, 45, 4) Largest integer: 998764543431 Numbers: (54, 546, 548, 60) Largest integer: 6054854654
Python: Compare repeated string method
def maxnum(x):
maxlen = len(str(max(x)))
return ''.join(sorted((str(v) for v in x), reverse=True,
key=lambda i: i*(maxlen * 2 // len(i))))
for numbers in [(212, 21221), (1, 34, 3, 98, 9, 76, 45, 4), (54, 546, 548, 60)]:
print('Numbers: %r\n Largest integer: %15s' % (numbers, maxnum(numbers)))
- Output:
Numbers: (212, 21221) Largest integer: 21221221 Numbers: (1, 34, 3, 98, 9, 76, 45, 4) Largest integer: 998764543431 Numbers: (54, 546, 548, 60) Largest integer: 6054854654
from fractions import Fraction
from math import log10
def maxnum(x):
return ''.join(str(n) for n in sorted(x, reverse=True,
key=lambda i: Fraction(i, 10**(int(log10(i))+1)-1)))
for numbers in [(1, 34, 3, 98, 9, 76, 45, 4), (54, 546, 548, 60)]:
print('Numbers: %r\n Largest integer: %15s' % (numbers, maxnum(numbers)))
- Output as first Python example, above.
Python: Try all permutations method
from itertools import permutations
def maxnum(x):
return max(int(''.join(n) for n in permutations(str(i) for i in x)))
for numbers in [(1, 34, 3, 98, 9, 76, 45, 4), (54, 546, 548, 60)]:
print('Numbers: %r\n Largest integer: %15s' % (numbers, maxnum(numbers)))
- Output as above.
Quackery
With a string of space separated sequences of digits
[ sortwith
[ 2dup swap join
dip join $< ]
[] swap witheach join ] is largest-int ( [ --> $ )
$ '1 34 3 98 9 76 45 4' nest$ largest-int echo$ cr
$ '54 546 548 60' nest$ largest-int echo$
- Output:
998764543431 6054854654
With a nest of numbers
[ number$ dip number$ join $->n drop ] is conc ( n n --> n )
[ 2dup conc unrot swap conc < ] is conc> ( n n --> b )
[ sortwith conc>
$ "" swap
witheach [ number$ join ]
$->n drop ] is task ( [ --> n )
' [ [ 1 34 3 98 9 76 45 4 ]
[ 54 546 548 60 ] ]
witheach [ task echo cr ]
- Output:
998764543431 6054854654
R
Largest_int_from_concat_ints <- function(vec){
#recursive function for computing all permutations
perm <- function(vec) {
n <- length(vec)
if (n == 1)
return(vec)
else {
x <- NULL
for (i in 1:n){
x <- rbind(x, cbind(vec[i], perm(vec[-i])))
}
return(x)
}
}
permutations <- perm(vec)
concat <- as.numeric(apply(permutations, 1, paste, collapse = ""))
return(max(concat))
}
#Verify
Largest_int_from_concat_ints(c(54, 546, 548, 60))
Largest_int_from_concat_ints(c(1, 34, 3, 98, 9, 76, 45, 4))
Largest_int_from_concat_ints(c(93, 4, 89, 21, 73))
- Output:
[1] 6054854654 [1] 998764543431 [1] 938973421
Racket
#lang racket
(define (largest-int ns)
(string->number (apply ~a (sort ns (λ(x y) (string>? (~a x y) (~a y x)))))))
(map largest-int '((1 34 3 98 9 76 45 4) (54 546 548 60)))
;; -> '(998764543431 6054854654)
Raku
(formerly Perl 6)
sub maxnum(*@x) {
[~] @x.sort: -> $a, $b { $b ~ $a leg $a ~ $b }
}
say maxnum <1 34 3 98 9 76 45 4>;
say maxnum <54 546 548 60>;
- Output:
998764543431 6054854654
Red
Red []
foreach seq [[1 34 3 98 9 76 45 4] [54 546 548 60]] [
print rejoin sort/compare seq function [a b] [ (rejoin [a b]) > rejoin [b a] ]
]
- Output:
998764543431 6054854654
REXX
The algorithm used is based on exact comparisons (left to right) with right digit fill of the left digit.
This allows the integers to be of any size.
This REXX version works with any size integer (negative, zero, positive), and does some basic error checking to
verify that the numbers are indeed integers (and it also normalizes the integers).
The absolute value is used for negative numbers. No sorting of the numbers is required for the 1st two examples.
simple integers
/*REXX program constructs the largest integer from an integer list using concatenation.*/
@.=.; @.1 = 1 34 3 98 9 76 45 4 /*the 1st integer list to be used. */
@.2 = 54 546 548 60 /* " 2nd " " " " " */
@.3 = 4 45 54 5 /* " 3rd " " " " " */
w=0 /* [↓] process all the integer lists.*/
do j=1 while @.j\==.; z= space(@.j) /*keep truckin' until lists exhausted. */
w=max(w, length(z) ); $= /*obtain maximum width to align output.*/
do while z\=''; idx= 1; big= norm(1) /*keep examining the list until done.*/
do k=2 to words(z); #= norm(k) /*obtain an a number from the list. */
L= max(length(big), length(#) ) /*get the maximum length of the integer*/
if left(#, L, left(#, 1) ) <<= left(big, L, left(big, 1) ) then iterate
big= #; idx= k /*we found a new biggie (and the index)*/
end /*k*/ /* [↑] find max concatenated integer. */
z= delword(z, idx, 1) /*delete this maximum integer from list*/
$= $ || big /*append " " " ───► $. */
end /*while z*/ /* [↑] process all integers in a list.*/
say 'largest concatenatated integer from ' left( space(@.j), w) " is ─────► " $
end /*j*/ /* [↑] process each list of integers. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
norm: arg i; #= word(z, i); er= '***error***'; if left(#, 1)=="-" then #= substr(#, 2)
if \datatype(#,'W') then do; say er # "isn't an integer."; exit 13; end; return #/1
- output when using the default (internal) integer lists:
largest concatenatated integer from 1 34 3 98 9 76 45 4 is ─────► 998764543431 largest concatenatated integer from 54 546 548 60 is ─────► 6054854654 largest concatenatated integer from 4 45 54 5 is ─────► 554454
exponentiated integers
In REXX, a number such as 6.6e77 would be considered an integer if the (current) numeric digits is
large enough to express that number as an integer without the exponent.
The default for REXX is 9 decimal digits, but the norm function automatically uses enough decimal digits to
express the number as an integer.
This REXX version can handle any sized integer (most REXXes can handle up to around eight million decimal
digits, but displaying the result would be problematic for results wider than the display area).
/*REXX program constructs the largest integer from an integer list using concatenation.*/
@.=.; @.1 = 1 34 3 98 9 76 45 4 /*the 1st integer list to be used. */
@.2 = 54 546 548 60 /* " 2nd " " " " " */
@.3 = 4 45 54 5 /* " 3rd " " " " " */
@.4 = 4 45 54 5 6.6e77 /* " 4th " " " " " */
w= 0 /* [↓] process all the integer lists.*/
do j=1 while @.j\==.; z= space(@.j) /*keep truckin' until lists exhausted. */
w=max(w, length(z) ); $= /*obtain maximum width to align output.*/
do while z\=''; idx=1; big= norm(1) /*keep examining the list until done.*/
do k=2 to words(z); #= norm(k) /*obtain an a number from the list. */
L= max(length(big), length(#) ) /*get the maximum length of the integer*/
if left(#, L, left(#, 1) ) <<= left(big, L, left(big, 1) ) then iterate
big=#; idx= k /*we found a new biggie (and the index)*/
end /*k*/ /* [↑] find max concatenated integer. */
z= delword(z, idx, 1) /*delete this maximum integer from list*/
$= $ || big /*append " " " ───► $. */
end /*while z*/ /* [↑] process all integers in a list.*/
say 'largest concatenatated integer from ' left( space(@.j), w) " is " $
end /*j*/ /* [↑] process each list of integers. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
norm: arg i; #= word(z, i); er= '***error***'; if left(#, 1)=="-" then #= substr(#, 2)
if \datatype(#, 'N') then signal er13 /*go and tell err msg.*/
else #= # / 1 /*a #, so normalize it*/
if pos('E',#)>0 then do; parse var # mant "E" pow /*Has exponent? Expand*/
numeric digits pow + length(mand) /*expand digs, adjust#*/
end
if datatype(#, 'W') then return # / 1
er13: say er # "isn't an integer."; exit 13
- output when using the default (internal) integer lists:
(Output shown at three-quarter size.)
largest concatenatated integer from 1 34 3 98 9 76 45 4 is 998764543431 largest concatenatated integer from 54 546 548 60 is 6054854654 largest concatenatated integer from 4 45 54 5 is 554454 largest concatenatated integer from 4 45 54 5 6.6e77 is 660000000000000000000000000000000000000000000000000000000000000000000000000000554454
Alternate Version
Inspired by the previous versions.
/*REXX program constructs the largest integer from an integer list using concatenation.*/
l.=''; l.1 = '1 34 3 98 9 76 45 4' /*the 1st integer list to be used. */
l.2 = '54 546 548 60' /* " 2nd " " " " " */
l.3 = ' 4 45 54 5' /* " 3rd " " " " " */
l.4 = ' 4 45 54 5 6.6e77' /* " 4th " " " " " */
l.5 = ' 3 3 .2' /* " 5th " " " " " */
/*
soll.1=998764543431
soll.2=6054854654
soll.3=554454
soll.4=660000000000000000000000000000000000000000000000000000000000000000000000000000545454
*/
l_length=0
Do li=1 By 1 While l.li<>''
l_length=max(l_length,length(space(l.li)))
End
Do li=1 By 1 While l.li<>''
z=''
Do j=1 To words(l.li)
int=integer(word(l.li,j))
If int='?' Then Do
Say left(space(l.li),l_length) '-> ** invalid ** bad integer' word(l.li,j)
Iterate li
End
Else
z=z int
End
/*Say copies(' ',l_length) ' ' soll.li */
Say left(space(l.li),l_length) '->' largeint(l.li)
End
Exit
integer: Procedure
Numeric Digits 1000
Parse Arg z
If Datatype(z,'W') Then
Return z+0
Else
Return '?'
largeint:
result=''
Do While z<>'' /* [?] check the rest of the integers.*/
big=word(z,1); index=1; LB=length(big) /*assume that first integer is biggest.*/
do k=2 to words(z);
n=word(z,k) /*obtain an integer from the list. */
L=max(LB,length(n)) /*get the maximum length of the integer*/
if left(n,L,left(n,1))<<=left(big,L,left(big,1)) then iterate
big=n; index=k /*we found a new biggie (and the index)*/
LB=length(big)
End /*k*/
z=delword(z,index,1) /*delete this maximum integer from list*/
result=result||big /*append " " " ---? $. */
end /*while z*/ /* [?] process all integers in a list.*/
Return result
- Output:
1 34 3 98 9 76 45 4 -> 998764543431 54 546 548 60 -> 6054854654 4 45 54 5 -> 554454 4 45 54 5 6.6e77 -> 660000000000000000000000000000000000000000000000000000000000000000000000000000554454 3 3 .2 -> ** invalid ** bad integer .2
Version 4
/*REXX program constructs the largest integer from an integer list using concatenation.*/
l.=''; l.1 = '1 34 3 98 9 76 45 4' /*the 1st integer list to be used. */
l.2 = '54 546 548 60' /* " 2nd " " " " " */
l.3 = ' 4 45 54 5' /* " 3rd " " " " " */
l.4 = ' 4 45 54 5 6.6e77' /* " 4th " " " " " */
l.5 = ' 3 3 .2' /* " 5th " " " " " */
l.6 = ' 4 45 54 5 6.6e1001' /* " 6th " " " " " */
l.7 = ' 4.0000 45 54 5.00' /* " 7th " " " " " */
l.8 = ' 10e999999999 5' /* " 8th " " " " " */
l_length=0
Do li=1 By 1 While l.li<>''
l_length=max(l_length,length(space(l.li)))
End
Do li=1 By 1 While l.li<>''
z=''
msg=''
Do j=1 To words(l.li)
int=integer(word(l.li,j))
If int='?' Then Do
Say left(space(l.li),l_length) '-> ** invalid ** bad list item:' word(l.li,j) msg
Iterate li
End
Else
z=z int
End
zz=largeint(z)
If length(zz)<60 Then
Say left(space(l.li),l_length) '->' zz
Else
Say left(space(l.li),l_length) '->' left(zz,5)'...'right(zz,5)
End
Exit
integer: Procedure Expose msg
Numeric Digits 1000
Parse Arg z
If Datatype(z,'W') Then
Return z/1
Else Do
If Datatype(z,'NUM') Then Do
Do i=1 To 6 Until dig>=999999999
dig= digits()*10
dig=min(dig,999999999)
Numeric Digits dig
If Datatype(z,'W') Then
Return z/1
End
msg='cannot convert it to an integer'
Return '?'
End
Else Do
msg='not a number (larger than what this REXX can handle)'
Return '?'
End
End
largeint: Procedure
Parse Arg list
w.0=words(list)
Do i=1 To w.0
w.i=word(list,i)
End
Do wx=1 To w.0-1
Do wy=wx+1 To w.0
xx=w.wx
yy=w.wy
xy=xx||yy
yx=yy||xx
if xy < yx then do
/* swap xx and yy */
w.wx = yy
w.wy = xx
end
End
End
list=''
Do ww=1 To w.0
list=list w.ww
End
Return space(list,0)
- Output:
1 34 3 98 9 76 45 4 -> 998764543431 54 546 548 60 -> 6054854654 4 45 54 5 -> 554454 4 45 54 5 6.6e77 -> 66000...54454 3 3 .2 -> ** invalid ** bad list item: .2 cannot convert it to an integer 4 45 54 5 6.6e1001 -> 66000...54454 4.0000 45 54 5.00 -> 554454 10e999999999 5 -> ** invalid ** bad list item: 10e999999999 not a number (larger than what this REXX can handle)
Ring
nums=[1,34,3,98,9,76,45,4]
see largestInt(8) + nl
nums=[54,546,548,60]
see largestInt(4) + nl
func largestInt len
l = ""
sorted = false
while not sorted
sorted=true
for i=1 to len-1
a=string(nums[i])
b=string(nums[i+1])
if a+b<b+a
temp = nums[i]
nums[i] = nums[i+1]
nums[i+1] = temp
sorted=false ok
next
end
for i=1 to len
l+=string(nums[i])
next
return l
Output:
998764543431 6054854654
RPL
We use here the second algorithm, easily derived from the SORT program given in Sorting algorithms/Bubble sort.
≪ LIST→ → len
≪ 1 len START
→STR len ROLL END
len 1 FOR n
1 n 1 - START
IF DUP2 + LAST SWAP + < THEN SWAP END
n ROLLD
NEXT n ROLLD
-1 STEP
2 len START + END STR→
≫ ≫ ‘MKBIG’ STO
{212 21221} MKBIG {1 34 3 98 9 76 45 4} MKBIG {54 546 548 60} MKBIG
- Output:
3: 21221221 2: 998764543431 1: 6054854654
Ruby
Sort on comparison of concatenated ints method
def icsort nums
nums.sort { |x, y| "#{y}#{x}" <=> "#{x}#{y}" }
end
[[54, 546, 548, 60], [1, 34, 3, 98, 9, 76, 45, 4]].each do |c|
p c # prints nicer in Ruby 1.8
puts icsort(c).join
end
- Output:
[54, 546, 548, 60] 6054854654 [1, 34, 3, 98, 9, 76, 45, 4] 998764543431
Compare repeated string method
def icsort nums
maxlen = nums.max.to_s.length
nums.map{ |x| x.to_s }.sort_by { |x| x * (maxlen * 2 / x.length) }.reverse
end
[[54, 546, 548, 60], [1, 34, 3, 98, 9, 76, 45, 4]].each do |c|
p c # prints nicer in Ruby 1.8
puts icsort(c).join
end
- Output as above.
require 'rational' #Only needed in Ruby < 1.9
def icsort nums
nums.sort_by { |i| Rational(i, 10**(Math.log10(i).to_i+1)-1) }.reverse
end
[[54, 546, 548, 60], [1, 34, 3, 98, 9, 76, 45, 4]].each do |c|
p c # prints nicer in Ruby 1.8
puts icsort(c).join
end
- Output as above.
Run BASIC
a1$ = "1, 34, 3, 98, 9, 76, 45, 4"
a2$ = "54,546,548,60"
print "Max Num ";a1$;" = ";maxNum$(a1$)
print "Max Num ";a2$;" = ";maxNum$(a2$)
function maxNum$(a1$)
while word$(a1$,i+1,",") <> ""
i = i + 1
a$(i) = trim$(word$(a1$,i,","))
wend
s = 1
while s = 1
s = 0
for j = 1 to i -1
if a$(j)+a$(j+1) < a$(j+1)+a$(j) then
h$ = a$(j)
a$(j) = a$(j+1)
a$(j+1) = h$
s = 1
end if
next j
wend
for j = 1 to i
maxNum$ = maxNum$ ; a$(j)
next j
end function
- Output:
Max Num 1, 34, 3, 98, 9, 76, 45, 4 = 998764543431 Max Num 54,546,548,60 = 6054854654
Rust
fn maxcat(a: &mut [u32]) {
a.sort_by(|x, y| {
let xy = format!("{}{}", x, y);
let yx = format!("{}{}", y, x);
xy.cmp(&yx).reverse()
});
for x in a {
print!("{}", x);
}
println!();
}
fn main() {
maxcat(&mut [1, 34, 3, 98, 9, 76, 45, 4]);
maxcat(&mut [54, 546, 548, 60]);
}
- Output:
998764543431 6054854654
S-lang
define catcmp(a, b)
{
a = string(a);
b = string(b);
return strcmp(b+a, a+b);
}
define maxcat(arr)
{
arr = arr[array_sort(arr, &catcmp)];
variable result = "", elem;
foreach elem (arr)
result += string(elem);
return result;
}
print("max of series 1 is " + maxcat([1, 34, 3, 98, 9, 76, 45, 4]));
print("max of series 2 is " + maxcat([54, 546, 548, 60]));
- Output:
"max of series 1 is 998764543431" "max of series 2 is 6054854654"
Scala
object LIFCI extends App {
def lifci(list: List[Long]) = list.permutations.map(_.mkString).max
println(lifci(List(1, 34, 3, 98, 9, 76, 45, 4)))
println(lifci(List(54, 546, 548, 60)))
}
- Output:
998764543431 6054854654
Scheme
(define (cat . nums) (apply string-append (map number->string nums)))
(define (my-compare a b) (string>? (cat a b) (cat b a)))
(map (lambda (xs) (string->number (apply cat (sort xs my-compare))))
'((1 34 3 98 9 76 45 4) (54 546 548 60)))
- Output:
(998764543431 6054854654)
Sidef
func maxnum(nums) {
nums.sort {|x,y| "#{y}#{x}" <=> "#{x}#{y}" };
}
[[54, 546, 548, 60], [1, 34, 3, 98, 9, 76, 45, 4]].each { |c|
say maxnum(c).join.to_num;
}
- Output:
6054854654 998764543431
Smalltalk
Version 1) sort by padded print strings:
#(
(54 546 548 60)
(1 34 3 98 9 76 45 4)
) do:[:ints |
|resultString|
"sort ints by padded strings (sort a copy - literals are immudatble),
then collect their strings, then concatenate"
resultString :=
((ints copy sort:[:a :b |
|pad|
pad := (a integerLog10) max:(b integerLog10).
(a printString paddedTo:pad with:$0) > (b printString paddedTo:pad with:$0)])
collect:#printString) asStringWith:''.
Stdout printCR: resultString.
].
Version 2) alternative: sort by concatenated pair's strings:
#(
(54 546 548 60)
(1 34 3 98 9 76 45 4)
) do:[:ints |
|resultString|
resultString :=
((ints copy sort:[:a :b | e'{a}{b}' > e'{b}{a}']) "(1)"
collect:#printString) asStringWith:''.
Stdout printCR: resultString.
].
Note ¹ replace "e'{a}{b}'" by "(a printString,b printString)" in dialects, which do not support embedded expression strings.
Version 3) no need to collect the resultString; simply print the sorted list (ok, if printing is all we want):
#(
(54 546 548 60)
(1 34 3 98 9 76 45 4)
) do:[:ints |
(ints copy sort:[:a :b | e'{a}{b}' > e'{b}{a}'])
do:[:eachNr | eachNr printOn:Stdout].
Stdout cr.
]
Version 4) no need to generate any intermediate strings; the following will do as well:
#(
(54 546 548 60)
(1 34 3 98 9 76 45 4)
) do:[:ints |
(ints copy sortByApplying:[:i | i log10 fractionPart]) reverseDo:#print.
Stdout cr.
]
- Output:
6054854654 989764543431
Tcl
proc intcatsort {nums} {
lsort -command {apply {{x y} {expr {"$y$x" - "$x$y"}}}} $nums
}
Demonstrating:
foreach collection {
{1 34 3 98 9 76 45 4}
{54 546 548 60}
} {
set sorted [intcatsort $collection]
puts "\[$collection\] => \[$sorted\] (concatenated: [join $sorted ""])"
}
- Output:
[1 34 3 98 9 76 45 4] => [9 98 76 45 4 34 3 1] (concatenated: 998764543431) [54 546 548 60] => [60 548 546 54] (concatenated: 6054854654)
Transd
#lang transd
MainModule: {
_start: (lambda
(for ar in [[98, 76, 45, 34, 9, 4, 3, 1], [54, 546, 548, 60]] do
(sort ar (λ l Int() r Int() (ret (> Int(String(l r)) Int(String(r l))))))
(lout (join ar "")))
)
}
- Output:
998764543431 6054854654
Uiua
A ← {[212 21221] [1 34 3 98 9 76 45 4][54 546 548 60]}
≡(⋕/◇⊂⊏⊸(⍖≡↯[/↥≡⧻]).°⋕)A
- Output:
[21221212 989764543431 6054854654]
VBScript
Function largestint(list)
nums = Split(list,",")
Do Until IsSorted = True
IsSorted = True
For i = 0 To UBound(nums)
If i <> UBound(nums) Then
a = nums(i)
b = nums(i+1)
If CLng(a&b) < CLng(b&a) Then
tmpnum = nums(i)
nums(i) = nums(i+1)
nums(i+1) = tmpnum
IsSorted = False
End If
End If
Next
Loop
For j = 0 To UBound(nums)
largestint = largestint & nums(j)
Next
End Function
WScript.StdOut.Write largestint(WScript.Arguments(0))
WScript.StdOut.WriteLine
- Output:
F:\>cscript /nologo largestint.vbs 1,34,3,98,9,76,45,4 998764543431 F:\>cscript /nologo largestint.vbs 54,546,548,60 6054854654
Vim Script
This solution is intended to be run as an Ex command within a buffer containing the integers to be processed, one per line.
%s/\(.\+\)/\1\1/ | sort! | %s/\(.\+\)\1\n/\1/
- Demonstration
$ paste -s nums
1 34 3 98 9 76 45 4
$ vim -S icsort.vim nums
998764543431
Wren
import "./sort" for Sort
var cmp = Fn.new { |x, y|
var xy = Num.fromString(x.toString + y.toString)
var yx = Num.fromString(y.toString + x.toString)
return (xy - yx).sign
}
var findLargestSequence = Fn.new { |a|
var b = Sort.merge(a, cmp)
return b[-1..0].join()
}
var arrays = [
[1, 34, 3, 98, 9, 76, 45, 4],
[54, 546, 548, 60]
]
for (a in arrays) {
System.print("%(a) -> %(findLargestSequence.call(a))")
}
- Output:
[1, 34, 3, 98, 9, 76, 45, 4] -> 998764543431 [54, 546, 548, 60] -> 6054854654
zkl
fcn bigCI(ns){
ns.apply("toString").sort(fcn(a,b){ (a+b)>(b+a) }).concat();
}
bigCI(T(1, 34, 3, 98, 9, 76, 45, 4)).println();
bigCI(T(54, 546, 548, 60)).println();
- Output:
998764543431 6054854654
- Programming Tasks
- Solutions by Programming Task
- 11l
- Ada
- Aime
- ALGOL 68
- Arturo
- AutoHotkey
- AWK
- BBC BASIC
- Bracmat
- C
- C sharp
- C++
- Ceylon
- Clojure
- Common Lisp
- D
- Delphi
- EasyLang
- Elixir
- Erlang
- F Sharp
- Factor
- Fortran
- FreeBASIC
- Frink
- Gambas
- Go
- Groovy
- Haskell
- Icon
- Unicon
- J
- Java
- JavaScript
- Jq
- Julia
- Kotlin
- Lua
- Mathematica
- Wolfram Language
- Maxima
- Min
- NetRexx
- Nim
- OCaml
- Oforth
- PARI/GP
- Pascal
- Perl
- Phix
- PHP
- Picat
- PicoLisp
- PL/I
- PowerShell
- Prolog
- Python
- Quackery
- R
- Racket
- Raku
- Red
- REXX
- Ring
- RPL
- Ruby
- Run BASIC
- Rust
- S-lang
- Scala
- Scheme
- Sidef
- Smalltalk
- Tcl
- Transd
- Uiua
- VBScript
- Vim Script
- Wren
- Wren-sort
- Zkl