Largest int from concatenated ints: Difference between revisions

Content added Content deleted
(→‎Tcl: Added implementation)
(→‎{{header|Python}}: translation to Perl6)
Line 8: Line 8:


Note: A solution could be to try 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.
Note: A solution could be to try 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.

=={{header|Perl 6}}==
{{trans|Python}}
<lang Perl 6>sub maxnum(@x) {
[~] sort -> $x, $y { $x~$y <=> $y~$x }, @x
}

say maxnum <1 34 3 98 9 76 45 4>;
say maxnum <54 546 548 60>;</lang>
{{out}}
<pre>998764543431
6054854654</pre>


=={{header|Python}}==
=={{header|Python}}==
Line 24: Line 36:
key=cmp_to_key(lambda x,y:cmp(int(y+x), int(x+y)))))
key=cmp_to_key(lambda x,y:cmp(int(y+x), int(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)))</lang>
print('Numbers: %r\n Largest integer: %15s' % (numbers, maxnum(numbers)))</lang>