Skip to content

Commit 6fd1479

Browse files
committed
Done with combination task.
1 parent 4fafdb7 commit 6fd1479

File tree

4 files changed

+90
-56
lines changed

4 files changed

+90
-56
lines changed

combination/index.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 generateCombinations = (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+
// Another solution
35+
// const generateCombinations = (digits) => {
36+
// let combinations = [''];
37+
38+
// for (let digit of digits) {
39+
// const letters = phoneDigits[digit];
40+
// const newCombinations = [];
41+
42+
// for (let combination of combinations) {
43+
// for (let letter of letters) {
44+
// newCombinations.push(combination + letter);
45+
// }
46+
// }
47+
48+
// combinations = newCombinations;
49+
// }
50+
51+
// return combinations;
52+
// }
53+
54+
// Using recursion to solve this generate combination problem.
55+
const generateCombinations = (digit) => {
56+
// define base class
57+
if (digit.length === 0) return [''];
58+
59+
const firstChar = digit[0];
60+
const remainingChars = digit.slice(1);
61+
62+
const result = phoneDigits[firstChar].flatMap(x => {
63+
const combination = generateCombinations(remainingChars);
64+
return combination.map(comb => x + comb);
65+
});
66+
67+
return result;
68+
};
69+
70+
module.exports = generateCombinations;

combination/test.js

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

recursion/index.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

recursion/test.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)