Skip to content

Instantly share code, notes, and snippets.

@1995eaton
Created July 3, 2014 05:53
Show Gist options
  • Save 1995eaton/913de7d43e2e97df7c5d to your computer and use it in GitHub Desktop.
Save 1995eaton/913de7d43e2e97df7c5d to your computer and use it in GitHub Desktop.
challenge169
// /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