Skip to content

Commit 4fafdb7

Browse files
committed
Completed naive solution for phoneKeyPadMatch.
1 parent 7b10d5c commit 4fafdb7

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

recursion/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @author: Chisimdi Damian Ezeanieto
3+
* @data: 11/03/2023
4+
*/
5+
6+
// Given an input '23', return all possible character match in a key pad match
7+
// e.g input: '23', output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
8+
const phoneDigits = {
9+
"2": ['a', 'b', 'c'],
10+
"3": ['d', 'e', 'f'],
11+
"4": ['g', 'h', 'i'],
12+
"5": ['j', 'k', 'l'],
13+
"6": ['m', 'n', 'o'],
14+
"7": ['p', 'q', 'r', 's'],
15+
"8": ['t', 'u', 'v'],
16+
"9": ['w', 'x', 'y', 'z'],
17+
};
18+
const phoneKeyPadMatch = (digit) => {
19+
const firstDigit = digit[0];
20+
const remainingDigit = digit.substring(1);
21+
22+
let chars = [];
23+
phoneDigits[firstDigit].map(firstChar => {
24+
for (let i = 0; i < remainingDigit.length; i++) {
25+
let remainingPhoneDigit = phoneDigits[remainingDigit[i]];
26+
27+
const combination = remainingPhoneDigit.map(comb => firstChar + comb);
28+
chars.push(combination);
29+
}
30+
});
31+
return chars.flatMap(x => x);
32+
};
33+
34+
module.exports = phoneKeyPadMatch;

recursion/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @author: Chisimdi Damian Ezeanieto
3+
* @date: 11/03/2023
4+
*/
5+
const phoneKeyPadMatch = require('./index');
6+
7+
describe('Phone key pad match', () => {
8+
test('phoneKeyPadMatch function is defined', () => {
9+
expect(typeof phoneKeyPadMatch).toEqual('function');
10+
});
11+
test('Calculate correct phoneKeyPadMatch value for "23" === ["ad","ae","af","bd","be","bf","cd","ce","cf"]', () => {
12+
expect(phoneKeyPadMatch('23')).toEqual(["ad","ae","af","bd","be","bf","cd","ce","cf"]);
13+
});
14+
test("Calculate correct phoneKeyPadMatch value for '234' === [ 'ad', 'ae', 'af', 'ag','ah', 'ai', 'bd', 'be','bf', 'bg', 'bh', 'bi','cd', 'ce', 'cf', 'cg','ch', 'ci']", () => {
15+
expect(phoneKeyPadMatch('234')).toEqual([
16+
'ad', 'ae', 'af', 'ag','ah', 'ai', 'bd', 'be','bf', 'bg', 'bh', 'bi','cd', 'ce', 'cf', 'cg','ch', 'ci'
17+
]);
18+
});
19+
test("Calculate correct phoneKeyPadMatch value for 'aaaa' & 'aa' === ['dg', 'dh', 'di', 'eg', 'eh', 'ei', 'fg', 'fh', 'fi']", () => {
20+
expect(phoneKeyPadMatch('34')).toEqual(['dg', 'dh', 'di', 'eg', 'eh', 'ei', 'fg', 'fh', 'fi']);
21+
});
22+
});

0 commit comments

Comments
 (0)