+ 12

[Challenge] - maximal number

Make a program that takes a list of integers and reorders them to create the largest integer possible from concatenating them Example: 50,22,3,9 becomes 950322 (Question 4 out of “Five programming problems every Software Engineer should be able to solve in less than 1 hour”)

17th Mar 2018, 4:19 AM
Daniel Peleg
Daniel Peleg - avatar
15 Answers
+ 14
Remember that you can submit this as an assignment using Lesson Factory. :>
17th Mar 2018, 4:23 AM
Hatsy Rei
Hatsy Rei - avatar
17th Mar 2018, 11:44 PM
LukArToDo
LukArToDo - avatar
+ 7
https://code.sololearn.com/cm8u7tfjRVS7/?ref=app
18th Mar 2018, 5:53 PM
D_Stark
D_Stark - avatar
17th Mar 2018, 5:45 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 5
Here is my solution in python https://code.sololearn.com/c28rvOlF18eE/?ref=app Extra challenge: Make sure your solution covers the case of: 420,42,423
17th Mar 2018, 4:23 AM
Daniel Peleg
Daniel Peleg - avatar
+ 5
@Daniel Peleg There still is a trouble in your method : take this input : a = "2,32,1,9,50,509,5013" your code gives 95050950133221 as the solution while 95095050133221 is bigger. the '9' in '509' is involved. A simple brute force way to solve the question is : a = "2,32,1,9,50,509,5013" print(max(''.join(i)for i in permutations(a.split(',')))) (or you can replace 'a' with 'input()' if you prefer - I don't). But an elegant, non-brute-force solution would be nicer.
17th Mar 2018, 4:31 PM
Cépagrave
Cépagrave - avatar
18th Mar 2018, 4:09 AM
VISHAL SRIVASTAVA
VISHAL SRIVASTAVA - avatar
+ 3
@Daniel You can get to the Lesson Factory by accessing the button with three dots at the top-right corner of your screen, at your launch screen (usually your activity feed). It is next to your notification button, the one which leads to "Settings".
17th Mar 2018, 4:34 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
@Cestpasgrave you are right. I have to add another condition to the if statement edited it. now it's right the conditions are quite hard to read because that line is a bit long, but the logic should be clear enough
17th Mar 2018, 8:38 PM
Daniel Peleg
Daniel Peleg - avatar
+ 2
@Daniel Peleg To answer 'how do I do that', you can read here : https://www.sololearn.com/Discuss/1137151/?ref=app it's about Lesson Factory
17th Mar 2018, 5:24 PM
Cépagrave
Cépagrave - avatar
20th Mar 2018, 4:09 AM
sayan chandra
sayan chandra - avatar
+ 1
How do I do that ?
17th Mar 2018, 4:25 AM
Daniel Peleg
Daniel Peleg - avatar
+ 1
sounds a lot like a sorting algorithm...
18th Mar 2018, 11:37 PM
Julian
Julian - avatar
+ 1
Very late I know - I'm definitely not a software engineer! https://code.sololearn.com/cBkIDh0HaLHM/?ref=app
30th Mar 2018, 6:59 PM
Russ
Russ - avatar
0
https://code.sololearn.com/c00j3prC07E4/#py I did this and later found out that I read the question wrong. This is a way to get the maximal number from the combination of all the digits in the list. Will attempt the actual problem now :P lol
17th Mar 2018, 11:16 AM
Pallav Gurung