Largest int from concatenated ints: Difference between revisions
Content added Content deleted
(→{{header|Frink}}: Shortened Frink) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 24: | Line 24: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F maxnum(x) |
||
V maxlen = String(max(x)).len |
V maxlen = String(max(x)).len |
||
R sorted((x.map(v -> String(v))), key' i -> i * (@maxlen * 2 I/ i.len), reverse' 1B).join(‘’) |
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]] |
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)))</ |
print("Numbers: #.\n Largest integer: #15".format(numbers, maxnum(numbers)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 45: | 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 52: | 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 83: | 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}}== |
=={{header|Aime}}== |
||
< |
<syntaxhighlight lang="aime">largest(...) |
||
{ |
{ |
||
index x; |
index x; |
||
Line 101: | Line 101: | ||
largest(54, 546, 548, 60); |
largest(54, 546, 548, 60); |
||
0; |
0; |
||
}</ |
}</syntaxhighlight> |
||
works for input up to 999999999. |
works for input up to 999999999. |
||
{{Out}} |
{{Out}} |
||
Line 109: | Line 109: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Using method 2 - first sorting into first digit order and then comparing concatenated pairs. |
Using method 2 - first sorting into first digit order and then comparing concatenated pairs. |
||
< |
<syntaxhighlight lang="algol68">BEGIN |
||
# returns the integer value of s # |
# returns the integer value of s # |
||
OP TOINT = ( STRING s)INT: |
OP TOINT = ( STRING s)INT: |
||
Line 177: | Line 177: | ||
print( ( newline ) ) |
print( ( newline ) ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 185: | Line 185: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">largestConcInt: function [arr]-> |
||
max map permutate arr 's [ |
max map permutate arr 's [ |
||
to :integer join map s => [to :string] |
to :integer join map s => [to :string] |
||
Line 191: | Line 191: | ||
loop [[1 34 3 98 9 76 45 4] [54 546 548 60]] 'a -> |
loop [[1 34 3 98 9 76 45 4] [54 546 548 60]] 'a -> |
||
print largestConcInt 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 204: | 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 212: | 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 220: | 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 240: | 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 246: | 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 266: | Line 266: | ||
l$+=STR$Nums%(i%) |
l$+=STR$Nums%(i%) |
||
NEXT |
NEXT |
||
=l$</ |
=l$</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
Line 272: | Line 272: | ||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<syntaxhighlight lang="bracmat">( ( maxnum |
||
= A Z F C |
= A Z F C |
||
. !arg:# |
. !arg:# |
||
Line 289: | 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 295: | 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 325: | Line 325: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 332: | Line 332: | ||
=={{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 366: | Line 366: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 373: | Line 373: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <sstream> |
#include <sstream> |
||
#include <algorithm> |
#include <algorithm> |
||
Line 401: | Line 401: | ||
<< " !\n" ; |
<< " !\n" ; |
||
return 0 ; |
return 0 ; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 410: | Line 410: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{works with|Ceylon|1.3.3}} |
{{works with|Ceylon|1.3.3}} |
||
< |
<syntaxhighlight lang="ceylon">shared void run() { |
||
function comparator(Integer x, Integer y) { |
function comparator(Integer x, Integer y) { |
||
Line 425: | Line 425: | ||
value test2 = {54, 546, 548, 60}; |
value test2 = {54, 546, 548, 60}; |
||
print(biggestConcatenation(test2)); |
print(biggestConcatenation(test2)); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(defn maxcat [coll] |
||
(read-string |
(read-string |
||
(apply str |
(apply str |
||
Line 436: | 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}} |
||
Line 444: | Line 444: | ||
=== Sort by two-by-two comparison of largest concatenated result === |
=== Sort by two-by-two comparison of largest concatenated result === |
||
< |
<syntaxhighlight lang="lisp"> |
||
(defun int-concat (ints) |
(defun int-concat (ints) |
||
(read-from-string (format nil "~{~a~}" ints))) |
(read-from-string (format nil "~{~a~}" ints))) |
||
Line 453: | Line 453: | ||
(defun make-largest-int (ints) |
(defun make-largest-int (ints) |
||
(int-concat (sort ints #'by-biggest-result))) |
(int-concat (sort ints #'by-biggest-result))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 467: | Line 467: | ||
=== Variation around the sort with padded most significant digit === |
=== 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 |
;; Sort criteria is by most significant digit with least digits used as a tie |
||
;; breaker |
;; breaker |
||
Line 493: | Line 493: | ||
#'largest-msd-with-less-digits))) |
#'largest-msd-with-less-digits))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 503: | Line 503: | ||
=={{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 523: | 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> |
||
Line 531: | Line 531: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">defmodule RC do |
||
def largest_int(list) do |
def largest_int(list) do |
||
sorted = Enum.sort(list, fn x,y -> "#{x}#{y}" >= "#{y}#{x}" end) |
sorted = Enum.sort(list, fn x,y -> "#{x}#{y}" >= "#{y}#{x}" end) |
||
Line 539: | Line 539: | ||
IO.inspect RC.largest_int [1, 34, 3, 98, 9, 76, 45, 4] |
IO.inspect RC.largest_int [1, 34, 3, 98, 9, 76, 45, 4] |
||
IO.inspect RC.largest_int [54, 546, 548, 60]</ |
IO.inspect RC.largest_int [54, 546, 548, 60]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 548: | Line 548: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
-module( largest_int_from_concatenated ). |
-module( largest_int_from_concatenated ). |
||
Line 560: | Line 560: | ||
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 569: | Line 569: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Form largest integer which is a permutation from a list of integers. Nigel Galloway: March 21st., 2018 |
// 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 |
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> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 581: | Line 581: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Using algorithm 3: |
Using algorithm 3: |
||
< |
<syntaxhighlight lang="factor">USING: assocs io kernel math qw sequences sorting ; |
||
IN: rosetta-code.largest-int |
IN: rosetta-code.largest-int |
||
Line 595: | Line 595: | ||
reverse concat print ; |
reverse concat print ; |
||
qw{ 1 34 3 98 9 76 45 4 } qw{ 54 546 548 60 } [ largest-int ] bi@</ |
qw{ 1 34 3 98 9 76 45 4 } qw{ 54 546 548 60 } [ largest-int ] bi@</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 604: | Line 604: | ||
Or alternatively, a translation of F#. |
Or alternatively, a translation of F#. |
||
{{trans|F#}} |
{{trans|F#}} |
||
< |
<syntaxhighlight lang="factor">USING: kernel math.order qw sequences sorting ; |
||
: fn ( seq -- str ) |
: fn ( seq -- str ) |
||
[ 2dup swap [ append ] 2bi@ after? +lt+ +gt+ ? ] sort concat ;</ |
[ 2dup swap [ append ] 2bi@ after? +lt+ +gt+ ? ] sort concat ;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 625: | Line 625: | ||
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 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. < |
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 |
INTEGER A,B,T |
||
T = B |
T = B |
||
Line 697: | Line 697: | ||
END DO !Thus, the numbers are aligned left in the text field. |
END DO !Thus, the numbers are aligned left in the text field. |
||
CALL BIGUP(T1,10) |
CALL BIGUP(T1,10) |
||
END </ |
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... |
Output: the Fortran compiler ignores spaces when reading fortran source, so, hard-core fortranners should have no difficulty doing likewise for the output... |
||
<pre> |
<pre> |
||
Line 720: | Line 720: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">#define MAXDIGITS 8 |
||
function catint( a as string, b as string ) as uinteger |
function catint( a as string, b as string ) as uinteger |
||
Line 762: | Line 762: | ||
sort_and_print(set1()) |
sort_and_print(set1()) |
||
sort_and_print(set2())</ |
sort_and_print(set2())</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
Line 769: | Line 769: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
< |
<syntaxhighlight lang="frink">a = [[1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60]] |
||
f = {|p| parseInt[join["",p]] } |
f = {|p| parseInt[join["",p]] } |
||
for s = a |
for s = a |
||
println[max[map[f, s.lexicographicPermute[]]]]</ |
println[max[map[f, s.lexicographicPermute[]]]]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 781: | Line 781: | ||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=4169e7641f29ff0ae1dd202b459e60ce Click this link to run this code]''' |
'''[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() |
Public Sub Main() |
||
Line 828: | Line 828: | ||
Print Val(sList.Join("")) 'Join all items in sList together and print |
Print Val(sList.Join("")) 'Join all items in sList together and print |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 836: | Line 836: | ||
=={{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 888: | Line 888: | ||
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 896: | Line 896: | ||
=={{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 912: | Line 912: | ||
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 918: | Line 918: | ||
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 928: | Line 928: | ||
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=== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (permutations) |
||
main :: IO () |
main :: IO () |
||
Line 947: | Line 947: | ||
(maxcat <$> [[1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60]] :: [Integer]) |
(maxcat <$> [[1, 34, 3, 98, 9, 76, 45, 4], [54, 546, 548, 60]] :: [Integer]) |
||
where |
where |
||
maxcat = read . maximum . fmap (concatMap show) . permutations</ |
maxcat = read . maximum . fmap (concatMap show) . permutations</syntaxhighlight> |
||
;Output as above. |
;Output as above. |
||
Line 956: | Line 956: | ||
lifting. |
lifting. |
||
< |
<syntaxhighlight lang="unicon">import Collections # For the Heap (dense priority queue) class |
||
procedure main(a) |
procedure main(a) |
||
Line 969: | Line 969: | ||
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 983: | Line 983: | ||
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: |
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 993: | Line 993: | ||
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 1,034: | Line 1,034: | ||
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 1,083: | Line 1,083: | ||
); |
); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
Line 1,092: | Line 1,092: | ||
===ES5=== |
===ES5=== |
||
< |
<syntaxhighlight lang="javascript"> (function () { |
||
'use strict'; |
'use strict'; |
||
Line 1,118: | Line 1,118: | ||
})(); |
})(); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 1,127: | Line 1,127: | ||
===ES6=== |
===ES6=== |
||
< |
<syntaxhighlight lang="javascript">var maxCombine = (a) => +(a.sort((x, y) => +("" + y + x) - +("" + x + y)).join('')); |
||
// test & output |
// test & output |
||
Line 1,133: | Line 1,133: | ||
[1, 34, 3, 98, 9, 76, 45, 4], |
[1, 34, 3, 98, 9, 76, 45, 4], |
||
[54, 546, 548, 60] |
[54, 546, 548, 60] |
||
].map(maxCombine));</ |
].map(maxCombine));</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 1,139: | Line 1,139: | ||
==== 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 1,152: | Line 1,152: | ||
([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 1,160: | Line 1,160: | ||
====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}}== |
=={{header|Julia}}== |
||
Perhaps algorithm 3 is more efficient, but algorithm 2 is decent and very easy to implement in Julia. So this solution uses algorithm 2. |
Perhaps algorithm 3 is more efficient, but algorithm 2 is decent and very easy to implement in Julia. So this solution uses algorithm 2. |
||
< |
<syntaxhighlight lang="julia">function maxconcat(arr::Vector{<:Integer}) |
||
b = sort(string.(arr); lt=(x, y) -> x * y < y * x, rev=true) |> join |
b = sort(string.(arr); lt=(x, y) -> x * y < y * x, rev=true) |> join |
||
return try parse(Int, b) catch parse(BigInt, b) end |
return try parse(Int, b) catch parse(BigInt, b) end |
||
Line 1,179: | Line 1,179: | ||
for arr in tests |
for arr in tests |
||
println("Max concatenating in $arr:\n -> ", maxconcat(arr)) |
println("Max concatenating in $arr:\n -> ", maxconcat(arr)) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,191: | Line 1,191: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="kotlin">import kotlin.Comparator |
||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
Line 1,206: | Line 1,206: | ||
println("%s -> %s".format(array.contentToString(), findLargestSequence(array))) |
println("%s -> %s".format(array.contentToString(), findLargestSequence(array))) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,216: | Line 1,216: | ||
{{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 1,225: | Line 1,225: | ||
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 1,233: | Line 1,233: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{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 1,239: | Line 1,239: | ||
(* 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 |
||
Line 1,246: | Line 1,246: | ||
=={{header|min}}== |
=={{header|min}}== |
||
{{works with|min|0.19.6}} |
{{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 |
('string map (over over swap s+ 's+ dip <) sort "" join int) :fn |
||
(1 34 3 98 9 76 45 4) fn puts! |
(1 34 3 98 9 76 45 4) fn puts! |
||
(54 546 548 60) fn puts!</ |
(54 546 548 60) fn puts!</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,258: | Line 1,258: | ||
=={{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 1,305: | Line 1,305: | ||
end il |
end il |
||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,313: | Line 1,313: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import algorithm, sequtils, strutils, sugar |
||
proc maxNum(x: seq[int]): string = |
proc maxNum(x: seq[int]): string = |
||
Line 1,321: | Line 1,321: | ||
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}} |
||
Line 1,328: | Line 1,328: | ||
=={{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 1,342: | Line 1,342: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">: largestInt map(#asString) sortWith(#[ 2dup + -rot swap + > ]) sum asInteger ;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,353: | Line 1,353: | ||
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. |
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([1, 34, 3, 98, 9, 76, 45, 4]) |
||
large([54, 546, 548, 60])</ |
large([54, 546, 548, 60])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>%1 = 998764543431 |
<pre>%1 = 998764543431 |
||
Line 1,363: | Line 1,363: | ||
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 1,501: | Line 1,501: | ||
InsertData(tmpData[i],source3[i]); |
InsertData(tmpData[i],source3[i]); |
||
HighestInt(tmpData); |
HighestInt(tmpData); |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
Line 1,510: | Line 1,510: | ||
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,635: | Line 1,635: | ||
InsertData(tmpData[i],source3[i]); |
InsertData(tmpData[i],source3[i]); |
||
HighestInt(tmpData); |
HighestInt(tmpData); |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>9987645434310 |
<pre>9987645434310 |
||
Line 1,642: | Line 1,642: | ||
=={{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 |
||
Line 1,653: | Line 1,653: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<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;">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> |
||
Line 1,665: | Line 1,665: | ||
<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;">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> |
<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> |
||
Line 1,673: | Line 1,673: | ||
=={{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,679: | Line 1,679: | ||
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 |
||
Line 1,688: | Line 1,688: | ||
===permutation/2=== |
===permutation/2=== |
||
< |
<syntaxhighlight lang="picat">s_perm1(L, Num) => |
||
permutation(L,P), |
permutation(L,P), |
||
Num = [I.to_string() : I in P].flatten().to_integer().</ |
Num = [I.to_string() : I in P].flatten().to_integer().</syntaxhighlight> |
||
===Using permutations/1=== |
===Using permutations/1=== |
||
< |
<syntaxhighlight lang="picat">s_perm2(L, Num) => |
||
Perms = permutations(L), |
Perms = permutations(L), |
||
Num = max([ [I.to_string() : I in P].flatten().to_integer() : P in Perms]).</ |
Num = max([ [I.to_string() : I in P].flatten().to_integer() : P in Perms]).</syntaxhighlight> |
||
===Sort on concatenated numbers=== |
===Sort on concatenated numbers=== |
||
< |
<syntaxhighlight lang="picat">s_sort_conc(L,Num) => |
||
Num = [to_string(I) : I in qsort(L,f3)].join('').to_integer(). |
Num = [to_string(I) : I in qsort(L,f3)].join('').to_integer(). |
||
Line 1,713: | Line 1,713: | ||
qsort([H|T],F) = qsort([E : E in T, call(F,E,H)], F) |
qsort([H|T],F) = qsort([E : E in T, call(F,E,H)], F) |
||
++ [H] ++ |
++ [H] ++ |
||
qsort([E : E in T, not call(F,E,H)],F).</ |
qsort([E : E in T, not call(F,E,H)],F).</syntaxhighlight> |
||
===Extend each element to the largest length=== |
===Extend each element to the largest length=== |
||
< |
<syntaxhighlight lang="picat">s_extend(L,Num) => |
||
LS = [I.to_string() : I in L], |
LS = [I.to_string() : I in L], |
||
MaxLen = 2*max([I.length : I in LS]), |
MaxLen = 2*max([I.length : I in LS]), |
||
Line 1,732: | Line 1,732: | ||
% sort function for s_extend/2 |
% sort function for s_extend/2 |
||
f4(N1,N2) => N1[1].to_integer() >= N2[1].to_integer().</ |
f4(N1,N2) => N1[1].to_integer() >= N2[1].to_integer().</syntaxhighlight> |
||
===Test=== |
===Test=== |
||
< |
<syntaxhighlight lang="picat">import util. |
||
go => |
go => |
||
Line 1,764: | Line 1,764: | ||
s_extend(L,Num4), |
s_extend(L,Num4), |
||
println(s_extent=Num4), |
println(s_extent=Num4), |
||
nl.</ |
nl.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,793: | Line 1,793: | ||
===Testing larger instance=== |
===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. |
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), |
garbage_collect(100_000_000), |
||
_ = random2(), |
_ = random2(), |
||
Line 1,809: | Line 1,809: | ||
time(s_extend(L,_Num4)), |
time(s_extend(L,_Num4)), |
||
nl.</ |
nl.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,830: | Line 1,830: | ||
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,841: | Line 1,841: | ||
(> |
(> |
||
(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,852: | Line 1,852: | ||
=={{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,883: | Line 1,883: | ||
end largest_integer; |
end largest_integer; |
||
end Largest; |
end Largest; |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
54 546 548 60 |
54 546 548 60 |
||
Line 1,895: | Line 1,895: | ||
{{works with|PowerShell|2}} |
{{works with|PowerShell|2}} |
||
Using algorithm 3 |
Using algorithm 3 |
||
< |
<syntaxhighlight lang="powershell">Function Get-LargestConcatenation ( [int[]]$Integers ) |
||
{ |
{ |
||
# Get the length of the largest integer |
# Get the length of the largest integer |
||
Line 1,911: | Line 1,911: | ||
return $Integer |
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 |
||
Get-LargestConcatenation 54, 546, 548, 60, 54, 546, 548, 60</ |
Get-LargestConcatenation 54, 546, 548, 60, 54, 546, 548, 60</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
Line 1,923: | Line 1,923: | ||
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,932: | Line 1,932: | ||
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,943: | Line 1,943: | ||
===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,978: | Line 1,978: | ||
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,992: | Line 1,992: | ||
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 2,007: | Line 2,007: | ||
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 2,016: | Line 2,016: | ||
===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 2,022: | Line 2,022: | ||
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\n Largest integer: %15s' % (numbers, maxnum(numbers)))</ |
print('Numbers: %r\n Largest integer: %15s' % (numbers, maxnum(numbers)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,033: | Line 2,033: | ||
{{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 2,041: | Line 2,041: | ||
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. |
||
Line 2,059: | Line 2,059: | ||
===With a string of space separated sequences of digits=== |
===With a string of space separated sequences of digits=== |
||
< |
<syntaxhighlight lang="quackery">[ sortwith |
||
[ 2dup swap join |
[ 2dup swap join |
||
dip join $< ] |
dip join $< ] |
||
Line 2,065: | Line 2,065: | ||
$ '1 34 3 98 9 76 45 4' nest$ largest-int echo$ cr |
$ '1 34 3 98 9 76 45 4' nest$ largest-int echo$ cr |
||
$ '54 546 548 60' nest$ largest-int echo$</ |
$ '54 546 548 60' nest$ largest-int echo$</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,074: | Line 2,074: | ||
===With a nest of numbers=== |
===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 ) |
[ 2dup conc unrot swap conc < ] is conc> ( n n --> b ) |
||
Line 2,086: | Line 2,086: | ||
[ 54 546 548 60 ] ] |
[ 54 546 548 60 ] ] |
||
witheach [ task echo cr ] </ |
witheach [ task echo cr ] </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,094: | Line 2,094: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">Largest_int_from_concat_ints <- function(vec){ |
||
#recursive function for computing all permutations |
#recursive function for computing all permutations |
||
Line 2,119: | Line 2,119: | ||
Largest_int_from_concat_ints(c(1, 34, 3, 98, 9, 76, 45, 4)) |
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)) |
Largest_int_from_concat_ints(c(93, 4, 89, 21, 73)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,130: | Line 2,130: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang="racket"> |
|||
<lang Racket> |
|||
#lang racket |
#lang racket |
||
(define (largest-int ns) |
(define (largest-int ns) |
||
Line 2,136: | Line 2,136: | ||
(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}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang="raku" line>sub maxnum(*@x) { |
||
[~] @x.sort: -> $a, $b { $b ~ $a leg $a ~ $b } |
[~] @x.sort: -> $a, $b { $b ~ $a leg $a ~ $b } |
||
} |
} |
||
say maxnum <1 34 3 98 9 76 45 4>; |
say maxnum <1 34 3 98 9 76 45 4>; |
||
say maxnum <54 546 548 60>;</ |
say maxnum <54 546 548 60>;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
6054854654</pre> |
6054854654</pre> |
||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang="rebol">Red [] |
||
foreach seq [[1 34 3 98 9 76 45 4] [54 546 548 60]] [ |
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] ] |
print rejoin sort/compare seq function [a b] [ (rejoin [a b]) > rejoin [b a] ] |
||
] |
] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,169: | Line 2,169: | ||
The absolute value is used for negative numbers. No sorting of the numbers is required for the 1<sup>st</sup> two examples. |
The absolute value is used for negative numbers. No sorting of the numbers is required for the 1<sup>st</sup> two examples. |
||
===simple integers=== |
===simple integers=== |
||
< |
<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. */ |
@.=.; @.1 = 1 34 3 98 9 76 45 4 /*the 1st integer list to be used. */ |
||
@.2 = 54 546 548 60 /* " 2nd " " " " " */ |
@.2 = 54 546 548 60 /* " 2nd " " " " " */ |
||
Line 2,190: | Line 2,190: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
norm: arg i; #= word(z, i); er= '***error***'; if left(#, 1)=="-" then #= substr(#, 2) |
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</ |
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:}} |
{{out|output|text= when using the default (internal) integer lists:}} |
||
<pre> |
<pre> |
||
Line 2,207: | Line 2,207: | ||
This REXX version can handle any sized integer (most REXXes can handle up to around eight million decimal |
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). |
<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. */ |
@.=.; @.1 = 1 34 3 98 9 76 45 4 /*the 1st integer list to be used. */ |
||
@.2 = 54 546 548 60 /* " 2nd " " " " " */ |
@.2 = 54 546 548 60 /* " 2nd " " " " " */ |
||
Line 2,235: | Line 2,235: | ||
end |
end |
||
if datatype(#, 'W') then return # / 1 |
if datatype(#, 'W') then return # / 1 |
||
er13: say er # "isn't an integer."; exit 13</ |
er13: say er # "isn't an integer."; exit 13</syntaxhighlight> |
||
{{out|output|text= when using the default (internal) integer lists:}} |
{{out|output|text= when using the default (internal) integer lists:}} |
||
Line 2,249: | Line 2,249: | ||
===Alternate Version=== |
===Alternate Version=== |
||
Inspired by the previous versions. |
Inspired by the previous versions. |
||
<lang>/*REXX program constructs the largest integer from an integer list using concatenation.*/ |
<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.=''; 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.2 = '54 546 548 60' /* " 2nd " " " " " */ |
||
Line 2,304: | Line 2,304: | ||
result=result||big /*append " " " ---? $. */ |
result=result||big /*append " " " ---? $. */ |
||
end /*while z*/ /* [?] process all integers in a list.*/ |
end /*while z*/ /* [?] process all integers in a list.*/ |
||
Return result</ |
Return result</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 34 3 98 9 76 45 4 -> 998764543431 |
<pre>1 34 3 98 9 76 45 4 -> 998764543431 |
||
Line 2,314: | Line 2,314: | ||
===Version 4=== |
===Version 4=== |
||
{{trans|NetRexx}} |
{{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.=''; 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.2 = '54 546 548 60' /* " 2nd " " " " " */ |
||
Line 2,394: | Line 2,394: | ||
list=list w.ww |
list=list w.ww |
||
End |
End |
||
Return space(list,0)</ |
Return space(list,0)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 34 3 98 9 76 45 4 -> 998764543431 |
<pre>1 34 3 98 9 76 45 4 -> 998764543431 |
||
Line 2,406: | Line 2,406: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
nums=[1,34,3,98,9,76,45,4] |
nums=[1,34,3,98,9,76,45,4] |
||
see largestInt(8) + nl |
see largestInt(8) + nl |
||
Line 2,431: | Line 2,431: | ||
next |
next |
||
return l |
return l |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,443: | Line 2,443: | ||
{{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 2,450: | Line 2,450: | ||
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 2,460: | Line 2,460: | ||
===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 2,468: | Line 2,468: | ||
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 2,481: | Line 2,481: | ||
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 2,514: | Line 2,514: | ||
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 |
||
Line 2,520: | Line 2,520: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn maxcat(a: &mut [u32]) { |
||
a.sort_by(|x, y| { |
a.sort_by(|x, y| { |
||
let xy = format!("{}{}", x, y); |
let xy = format!("{}{}", x, y); |
||
Line 2,535: | Line 2,535: | ||
maxcat(&mut [1, 34, 3, 98, 9, 76, 45, 4]); |
maxcat(&mut [1, 34, 3, 98, 9, 76, 45, 4]); |
||
maxcat(&mut [54, 546, 548, 60]); |
maxcat(&mut [54, 546, 548, 60]); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>998764543431 |
<pre>998764543431 |
||
Line 2,541: | Line 2,541: | ||
=={{header|S-lang}}== |
=={{header|S-lang}}== |
||
< |
<syntaxhighlight lang="s-lang">define catcmp(a, b) |
||
{ |
{ |
||
a = string(a); |
a = string(a); |
||
Line 2,559: | Line 2,559: | ||
print("max of series 1 is " + maxcat([1, 34, 3, 98, 9, 76, 45, 4])); |
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])); |
print("max of series 2 is " + maxcat([54, 546, 548, 60])); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>"max of series 1 is 998764543431" |
<pre>"max of series 1 is 998764543431" |
||
Line 2,567: | Line 2,567: | ||
=={{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 2,573: | Line 2,573: | ||
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 2,582: | Line 2,582: | ||
=={{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 2,595: | Line 2,595: | ||
=={{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 2,601: | Line 2,601: | ||
[[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 2,611: | Line 2,611: | ||
Version 1) sort by padded print strings: |
Version 1) sort by padded print strings: |
||
{{works with|Smalltalk/X}} |
{{works with|Smalltalk/X}} |
||
< |
<syntaxhighlight lang="smalltalk">#( |
||
(54 546 548 60) |
(54 546 548 60) |
||
(1 34 3 98 9 76 45 4) |
(1 34 3 98 9 76 45 4) |
||
Line 2,626: | Line 2,626: | ||
collect:#printString) asStringWith:''. |
collect:#printString) asStringWith:''. |
||
Stdout printCR: resultString. |
Stdout printCR: resultString. |
||
].</ |
].</syntaxhighlight> |
||
Version 2) alternative: sort by concatenated pair's strings: |
Version 2) alternative: sort by concatenated pair's strings: |
||
< |
<syntaxhighlight lang="smalltalk">#( |
||
(54 546 548 60) |
(54 546 548 60) |
||
(1 34 3 98 9 76 45 4) |
(1 34 3 98 9 76 45 4) |
||
Line 2,638: | Line 2,638: | ||
collect:#printString) asStringWith:''. |
collect:#printString) asStringWith:''. |
||
Stdout printCR: resultString. |
Stdout printCR: resultString. |
||
].</ |
].</syntaxhighlight> |
||
Note ¹ replace "e'{a}{b}'" by "(a printString,b printString)" in dialects, which do not support embedded expression strings. |
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): |
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) |
(54 546 548 60) |
||
(1 34 3 98 9 76 45 4) |
(1 34 3 98 9 76 45 4) |
||
Line 2,649: | Line 2,649: | ||
do:[:eachNr | eachNr printOn:Stdout]. |
do:[:eachNr | eachNr printOn:Stdout]. |
||
Stdout cr. |
Stdout cr. |
||
]</ |
]</syntaxhighlight> |
||
Version 4) no need to generate any intermediate strings; the following will do as well: |
Version 4) no need to generate any intermediate strings; the following will do as well: |
||
< |
<syntaxhighlight lang="smalltalk">#( |
||
(54 546 548 60) |
(54 546 548 60) |
||
(1 34 3 98 9 76 45 4) |
(1 34 3 98 9 76 45 4) |
||
Line 2,658: | Line 2,658: | ||
(ints copy sortByApplying:[:i | i log10 fractionPart]) reverseDo:#print. |
(ints copy sortByApplying:[:i | i log10 fractionPart]) reverseDo:#print. |
||
Stdout cr. |
Stdout cr. |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>6054854654 |
<pre>6054854654 |
||
Line 2,664: | Line 2,664: | ||
=={{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 2,674: | Line 2,674: | ||
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> |
||
Line 2,683: | Line 2,683: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
{{trans|BBC BASIC}} |
{{trans|BBC BASIC}} |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Function largestint(list) |
Function largestint(list) |
||
nums = Split(list,",") |
nums = Split(list,",") |
||
Line 2,708: | Line 2,708: | ||
WScript.StdOut.Write largestint(WScript.Arguments(0)) |
WScript.StdOut.Write largestint(WScript.Arguments(0)) |
||
WScript.StdOut.WriteLine |
WScript.StdOut.WriteLine |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 2,721: | Line 2,721: | ||
=={{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}}== |
=={{header|Wren}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{libheader|Wren-sort}} |
{{libheader|Wren-sort}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/sort" for Sort |
||
var cmp = Fn.new { |x, y| |
var cmp = Fn.new { |x, y| |
||
Line 2,752: | Line 2,752: | ||
for (a in arrays) { |
for (a in arrays) { |
||
System.print("%(a) -> %(findLargestSequence.call(a))") |
System.print("%(a) -> %(findLargestSequence.call(a))") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,761: | Line 2,761: | ||
=={{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> |