Created
December 8, 2015 01:44
-
-
Save y7kim/6b92e5ea6e267afb2096 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 keypadPermutations = function (phone_number) { | |
// Split phone number into array and parseInt each | |
var digits = phone_number.split('').map(function(digit) { | |
return parseInt(digit); | |
}); | |
// Map letters to keys | |
var KEYMAPS = [ | |
['0'], // 0, | |
['1'], // 1, | |
['a', 'b', 'c'], // 2 | |
['d', 'e', 'f'], // 3 | |
['g', 'h', 'i'], // 4 | |
['j', 'k', 'l'], // 5 | |
['m', 'n', 'o'], // 6 | |
['p', 'q', 'r', 's'], // 7 | |
['t', 'u', 'v'], // 8 | |
['w', 'x', 'y', 'z'] // 9 | |
]; | |
// Array to be populated with all possible sequences | |
var solution = []; | |
(function sequencer(progress, i) { | |
if ((i === phone_number.length - 1)) { | |
KEYMAPS[digits[i]].forEach(function(letter) { | |
solution.push(progress + letter); | |
}); | |
} else { | |
KEYMAPS[digits[i]].forEach(function(letter) { | |
sequencer(progress + letter, i+1); | |
}); | |
} | |
})('', 0); // Start with empty string and first digit | |
return solution; | |
}; | |
module.exports = keypadPermutations; |
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 keypadPermutations = require('./keypad.js'); | |
describe('keypadPermutations', function() { | |
it('works for 2 digits', function() { | |
var perms = keypadPermutations('23'); | |
var result = ['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf']; | |
expect(perms).toEqual(result); | |
}); | |
it('works for 3 digits', function() { | |
var perms = keypadPermutations('234'); | |
var result = ['adg', 'adh', 'adi', 'aeg', 'aeh', 'aei', 'afg', 'afh', 'afi', 'bdg', 'bdh', 'bdi', 'beg', 'beh', 'bei', 'bfg', 'bfh', 'bfi','cdg', 'cdh', 'cdi', 'ceg', 'ceh', 'cei', 'cfg', 'cfh', 'cfi']; | |
expect(perms).toEqual(result); | |
}); | |
it('works for number with 0', function() { | |
var perms = keypadPermutations('023'); | |
var result = ['0ad', '0ae', '0af', '0bd', '0be', '0bf', '0cd', '0ce', '0cf']; | |
expect(perms).toEqual(result); | |
}); | |
it('works for number with 1', function() { | |
var perms = keypadPermutations('231'); | |
var result = ['ad1', 'ae1', 'af1', 'bd1', 'be1', 'bf1', 'cd1', 'ce1', 'cf1']; | |
expect(perms).toEqual(result); | |
}); | |
it('works for 7 digits of 3 letters each', function() { | |
var perms = keypadPermutations('2345682'); | |
expect(perms.length).toEqual(2187); // 3^7 = 2187 | |
}); | |
it('works for 7 digits of mixed number of letters each', function() { | |
var perms = keypadPermutations('2345678'); | |
expect(perms.length).toEqual(2916); // 3^6 * 4 = 2916 | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment