Created
July 3, 2014 05:53
-
-
Save 1995eaton/913de7d43e2e97df7c5d to your computer and use it in GitHub Desktop.
challenge169
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
// /r/dailyprogrammer challenge #169 (http://redd.it/29od55) | |
// Word list from https://dotnetperls-controls.googlecode.com/files/enable1.txt | |
var wordlist = require('fs').readFileSync('./dict', 'utf8').split(/\n+/) | |
.filter(function(e) { return e; }); | |
console.log(challenge169(process.argv[2] || 'A oweaib who fprd not zfqzh challenges should mt ewlst to kze', wordlist)); | |
function challenge169(string, wordlist) { | |
var shift = function(characters, start, end) { | |
for (var n = start, _ret = []; n < end; n++) { | |
_ret.push(characters.split('').map(function(e) { | |
var row = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm'] | |
.filter(function(r) { return r.indexOf(e) !== -1; }).pop(); | |
return row[(((row.indexOf(e) + n) % row.length) + row.length) % row.length]; | |
}).join('')); | |
} | |
return _ret; | |
}; | |
return string.replace(/\b[a-zA-Z]+\b/g, function(word) { | |
if (wordlist.indexOf(word.toLowerCase()) !== -1) { | |
return word; | |
} | |
return '{' + shift(word.toLowerCase(), -2, 3).filter(function(e) { | |
return wordlist.indexOf(e) !== -1; | |
}).join(', ') + '}'; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment