+ 3

Challenge : Find largest formed number from given numbers in an array.

Write a program that accepts list of non negative integers, arranges them such that they form the largest possible number. For example, given [50, 2, 1, 9], the largest formed number is 95021.

1st Aug 2017, 8:48 AM
Kartikey Sahu
Kartikey Sahu - avatar
14 Réponses
1st Aug 2017, 9:19 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 4
my view on the challenge. https://code.sololearn.com/cZ24Q62ArDRS/?ref=app (bug for [2 21 29] fixed.) thanks to @Stephen for pointing out.
1st Aug 2017, 10:34 AM
yuri
+ 4
Everyone doing this challenge should try [10,1] as an input. The correct answer for this is [1,10] (which gives 110 as the formed number). If you're just string sorting the array, it won't work for this input in many languages. Credit to Yuri for posting this as a comment on Domagoj's code. Other inputs that also find the same flaw: [a, ab] where ab is a 2 digit number and a>b.
1st Aug 2017, 10:51 AM
Stephen
+ 2
@stephen you're right, mine doest actually work didn't think about that
1st Aug 2017, 10:53 AM
Jordan Chapman
Jordan Chapman - avatar
1st Aug 2017, 11:44 AM
Jordan Chapman
Jordan Chapman - avatar
+ 2
Here is my try with python Just input numbers of list with one space https://code.sololearn.com/cGX0v3vOT749/?ref=app
1st Aug 2017, 5:47 PM
Harsh Prakash Agarwal
Harsh Prakash Agarwal - avatar
+ 2
It does not work with big arrays as long unsigned are limited in size, but at least it is a number (not a string) https://code.sololearn.com/c9Ea8erpeMDP/?ref=app
2nd Aug 2017, 11:46 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
1st Aug 2017, 1:49 PM
Garikai
Garikai - avatar
1st Aug 2017, 6:29 PM
Dennis
Dennis - avatar
1st Aug 2017, 6:03 PM
Garikai
Garikai - avatar
0
I have a slightly functional algorithm right now. So far, a good amount of the time, it works, but in a few instances I'm getting a maximum recursion depth exceeded error in Python. Once I figure that out, I will post mine Edit: I'm super close to finishing, but I'm going to class now. Should have it ready later tonight. Edit: Taking much longer than I thought. Once I fix one bug, another seems to show up. I will get this perfect though, mark my words.
1st Aug 2017, 6:56 PM
Vari93
Vari93 - avatar
0
I finished it. At last. Code will come soon, Overwatch calls me.
2nd Aug 2017, 7:11 AM
Vari93
Vari93 - avatar
0
Bam. Checks for valid input, and works for every possible size of array and any values given into the array (unless it gets time exceeded, not sure when it starts doing that) but it def works. This was surprisingly hard Edit: Removed the function of swapping each individual number to make it's biggest form. Domagoj mentioned that wasnt a part of this, I thought it was because of a comment on here that I misunderstood xP Works exactly how it should still, just without the swapping of individual numbers. https://code.sololearn.com/cZE00vUVUph1/?ref=app
2nd Aug 2017, 9:47 AM
Vari93
Vari93 - avatar