Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,31 @@
|
|||
var encodeMTF = function (word) {
|
||||
var init = {wordAsNumbers: [], charList: 'abcdefghijklmnopqrstuvwxyz'.split('')};
|
||||
|
||||
return word.split('').reduce(function (acc, char) {
|
||||
var charNum = acc.charList.indexOf(char); //get index of char
|
||||
acc.wordAsNumbers.push(charNum); //add original index to acc
|
||||
acc.charList.unshift(acc.charList.splice(charNum, 1)[0]); //put at beginning of list
|
||||
return acc;
|
||||
}, init).wordAsNumbers; //return number list
|
||||
};
|
||||
|
||||
var decodeMTF = function (numList) {
|
||||
var init = {word: '', charList: 'abcdefghijklmnopqrstuvwxyz'.split('')};
|
||||
|
||||
return numList.reduce(function (acc, num) {
|
||||
acc.word += acc.charList[num];
|
||||
acc.charList.unshift(acc.charList.splice(num, 1)[0]); //put at beginning of list
|
||||
return acc;
|
||||
}, init).word;
|
||||
};
|
||||
|
||||
//test our algorithms
|
||||
var words = ['broood', 'bananaaa', 'hiphophiphop'];
|
||||
var encoded = words.map(encodeMTF);
|
||||
var decoded = encoded.map(decodeMTF);
|
||||
|
||||
//print results
|
||||
console.log("from encoded:");
|
||||
console.log(encoded);
|
||||
console.log("from decoded:");
|
||||
console.log(decoded);
|
||||
Loading…
Add table
Add a link
Reference in a new issue