-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
one-line solution #1
base: master
Are you sure you want to change the base?
Conversation
The idea was to put on left the highest digits. So:
The instructions:
|
I realised that it doesn't work for some particular inputs :(. Input array: The algorithm sorts the array this way:
and then produces the output:
Example. Using the input array: The script gives |
@@ -17,5 +15,5 @@ XCTAssertEqual(90, largestNumberComposed(from: [9,0])) | |||
|
|||
XCTAssertEqual(910, largestNumberComposed(from: [10,9])) | |||
|
|||
XCTAssertEqual(95021, largestNumberComposed(from: [50,2,1,9])) | |||
XCTAssertEqual(56554, largestNumberComposed(from: [54,5,56])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you have pointed out, a lexicographic sort and concatenation is close, but not quite correct. For example, for the numbers 5, 54, and 56 a lexicographic sort will produce {5, 54, 56} (in increasing order) or {56, 54, 5} (in decreasing order), but what we really want is {56, 5, 54}, since that produces the largest number possible.
thanks to Marco Sciamanna... my first solution was actually a bubble-sort 馃憥