Skip to content

Instantly share code, notes, and snippets.

@kirbysayshi
Created February 28, 2011 23:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kirbysayshi/848312 to your computer and use it in GitHub Desktop.
Save kirbysayshi/848312 to your computer and use it in GitHub Desktop.
var input = 'FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth';
function sliceNDice(input){
var i
,pairs = [];
for(i = 0; i < input.length; i++){
if(i + 1 < input.length){
if(input[i] == input[i+1]) pairs.push( [i, i+1] );
}
}
recu(input, pairs, [0,0]);
}
function recu(input, pairs, longestRange){
var pair
,start
,end
,toTest
,newStart
,newEnd
,nextPairs = []
,rangeLength = longestRange[1] - longestRange[0]
,newRange = [];
for(i = 0; i < pairs.length; i++){
pair = pairs[i];
start = pair[0];
end = pair[1];
toTest = input.substring(start, end - start);
newStart = null;
newEnd = null;
if(start - 1 > -1) {
toTest = input[start-1] + toTest;
}
if(toTest == toTest.split('').reverse().join('')){
// found a palindrome by going back one
newStart = start - 1;
} else {
newStart = start;
}
if(end + 1 < input.length) {
toTest = toTest + input[end+1];
}
if(toTest == toTest.split('').reverse().join('')){
// found a palindrome by going forward one
newEnd = end + 1;
} else {
newEnd = end;
}
nextPairs.push( [newStart, newEnd] );
}
// find longest so far
for(i = 0; i < nextPairs.length; i++){
pair = nextPairs[i];
start = pair[0];
end = pair[1];
if(end - start > rangeLength){
newRange[0] = start;
newRange[1] = end;
rangeLength = end - start;
} else {
newRange = longestRange;
}
}
console.log('longest range: ', longestRange);
console.log('longest range as text: ', input.substring( longestRange[0], longestRange[1] - longestRange[0] ) );
recu(input, nextPairs, newRange);
}
sliceNDice(input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment