Created
May 18, 2010 15:47
-
-
Save buger/405144 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var towns = ['Калининград', 'Вологда', 'Алматы', 'Дмитров', 'Архангельск', 'Тобольск', 'Краков']; | |
var variants = []; | |
function get_town(town, towns, result, index){ | |
if(!index) | |
index = 1; | |
var i; | |
var suggested_towns = []; | |
for(i=0; i<towns.length; i++){ | |
if(towns[i] && result.indexOf(towns[i])==-1 && towns[i][0].toUpperCase() == town[town.length-index].toUpperCase()) | |
suggested_towns.push(towns[i]); | |
} | |
for(i=0; i<suggested_towns.length;i++){ | |
var new_result = result.slice(0); | |
new_result.push(suggested_towns[i]); | |
get_town(suggested_towns[i], towns, new_result); | |
} | |
if(suggested_towns.length == 0){ | |
if((town.length-index)!=0) | |
get_town(town, towns, result, index + 1); | |
else | |
variants.push(result); | |
} | |
} | |
function play(towns){ | |
for(var i=0; i<towns.length; i++){ | |
get_town(towns[i], towns, [towns[i]]); | |
} | |
return variants.sort(function(a,b){return a.length == b.length ? 0 : (a.length > b.length ? -1 : 1)})[0]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment