Created
November 17, 2012 09:43
-
-
Save paul-english/4094542 to your computer and use it in GitHub Desktop.
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
var assert = require('assert'); | |
function symmetryPoint ( S ) { | |
if (S.length % 2 === 0) { | |
return -1; | |
} else { | |
var middle = Math.floor(S.length / 2); | |
var front = S.substr(0, middle); | |
var back = S.substr(middle+1, S.length); | |
if (front === reverse(back)) { | |
return middle; | |
} else { | |
return -1; | |
} | |
} | |
function reverse(s){ | |
return s.split("").reverse().join(""); | |
} | |
} | |
console.log('run'); | |
var out1 = symmetryPoint("racecar"); | |
console.log('out1', out1); | |
assert.equal(out1, 3); | |
var out2 = symmetryPoint("x"); | |
console.log('out2', out2); | |
assert.equal(out2, 0); | |
var out3 = symmetryPoint("xy"); | |
console.log('out3', out3); | |
assert.equal(out3, -1); | |
var out4 = symmetryPoint("xyz"); | |
console.log('out4', out4); | |
assert.equal(out4, -1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment