Skip to content

Instantly share code, notes, and snippets.

@Havvy
Created April 16, 2015 12:28
Show Gist options
  • Select an option

  • Save Havvy/5a201c9b6398c2e60b68 to your computer and use it in GitHub Desktop.

Select an option

Save Havvy/5a201c9b6398c2e60b68 to your computer and use it in GitHub Desktop.
// Given a phone number (array of 10 digits), find all alphabetical permutations using the following digit mapping.
// Dont look up the answer, that ruins the fun!
var digitMap = {
0: ['0'],
1: ['1'],
2: ['a','b','c'],
3: ['d','e','f'],
4: ['g','h','i'],
5: ['j','k','l'],
6: ['m','n','o'],
7: ['p','q','r','s'],
8: ['t','u','v'],
9: ['w','x','y','z']
};
function objectToFunction (obj) {
return function (key) {
return obj[key];
};
}
function flatten (arrayOfArrays) {
return arrayOfArrays.reduce(function (res, array) {
return res.concat(array);
}, []);
}
function builder(phone){
// your code here
return phone.map(objectToFunction(digitMap))
.reduce(function (permutations, digitChars) {
return flatten(permutations.map(function (permutation) {
return digitChars.map(function (digitChar) {
return permutation + digitChar;
});
}));
});
}
var start = Date.now();
(builder([5,5,5,0,2,9,6,8,1,3]));
console.log(Date.now() - start);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment