Created
August 30, 2015 00:31
-
-
Save cernael/d2890ad366863f14e6cd to your computer and use it in GitHub Desktop.
Solution attempt (without bruteforcing) to the infamous problem 4 from https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
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
function maximizer(list){ | |
function sortIt(x, y){ | |
x += ''; | |
y += ''; | |
if(x === y){ | |
return 0; | |
} | |
if(x.length === y.length){ | |
if(x < y){ | |
return 1; | |
} | |
if(x > y){ | |
return -1; | |
} | |
} | |
if(x.length < y.length){ | |
var a = x, | |
b = y.slice(0, x.length), | |
c = y.slice(x.length), | |
test = sortIt(a, b); | |
if(test !== 0){ | |
return test; | |
} else { | |
return sortIt(b+c, c+a); | |
} | |
} | |
if(x.length > y.length){ | |
var a = x.slice(0, y.length), | |
b = x.slice(y.length), | |
c = y, | |
test = sortIt(a, c); | |
if(test !== 0){ | |
return test; | |
} else { | |
return sortIt(b+c, a+b); | |
} | |
} | |
} | |
return list.sort(sortIt).join('')/1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment