Largest int from concatenated ints: Difference between revisions

Content added Content deleted
(→‎{{header|Python}}: This also shows one of the few times where cmp= is better than key= on sorted())
(Slight layout change in task description. (No new info).)
Line 1: Line 1:
{{draft task}}
{{draft 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 number.
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.


Use the following two sets of integers as tests and show your program output here.
Use the following two sets of integers as tests and show your program output here.
Line 7: Line 7:
* {54, 546, 548, 60}
* {54, 546, 548, 60}


;Possible algorithms:
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.
# A solution could be found by trying all combinations and return the best.

Note: Yet another way to solve this is to pad ints to the same size by repeating the digits then sort using these repeated ints as a sort key.
# 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 ints to the same size by repeating the digits then sort using these repeated ints as a sort key.


Cf:
Cf: