Created
May 17, 2015 03:22
-
-
Save juanandresnyc/ab3309af0df4e329e862 to your computer and use it in GitHub Desktop.
Solution to P4 from Santiago L. Valdarrama
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour?utm_content=bufferf0b7c&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer | |
function getLargestNumber(list) { | |
var i, str, strLen; | |
var listLen = list.length; | |
var largestNumDigits = 0; | |
var strList = []; | |
var sameLengthList = []; | |
for (i = 0; i < listLen; i++) { | |
str = list[i].toString(); | |
strList[i] = str; | |
strLen = str.length; | |
if (strLen > largestNumDigits) largestNumDigits = strLen; | |
} | |
for (i = 0; i < listLen; i++) { | |
str = strList[i]; | |
for (var j = str.length; j < largestNumDigits; j++) { | |
str = str += '9'; | |
} | |
sameLengthList[i] = {index: i, number: parseInt(str, 10)}; | |
} | |
// TODO use mergesort here :P | |
sameLengthList = sameLengthList.sort(function(a, b) { | |
return (a.number < b.number); | |
}); | |
var result = ''; | |
for (i = 0; i < listLen; i++) { | |
result += strList[sameLengthList[i].index]; | |
} | |
return result; | |
} | |
console.log('largest', getLargestNumber([50, 2, 1, 9])); | |
console.log('largest', getLargestNumber([420, 42, 423])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment