Jump to content

Largest int from concatenated ints: Difference between revisions

Slight layout change in task description. (No new info).
(→‎{{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:
{{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 numberinteger.
 
Use the following two sets of integers as tests and show your program output here.
Line 7:
* {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 anotherAnother way to solve this is to padnote intsthat toin the samebest sizearrangement, byfor repeatingany two adjacent original integers X and Y, the digitsconcatenation thenX sortfollowed usingby theseY repeatedwill intsbe asnumerically agreater sortthan or equal to the concatenation Y followed by keyX.
# 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:
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.