Skip to content

Instantly share code, notes, and snippets.

@samrocksc
Created February 7, 2018 23:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samrocksc/95a48f953ab5a8b6e797d4101737d4ab to your computer and use it in GitHub Desktop.
Save samrocksc/95a48f953ab5a8b6e797d4101737d4ab to your computer and use it in GitHub Desktop.
phonepad randos
const phonepad = [
[],
[],
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i'],
['j', 'k', 'l'],
['m', 'n', 'o'],
['p', 'q', 'r', 's'],
['t', 'u', 'v'],
['w', 'x', 'y', 'z'],
]
const combine = function(a, min) {
const fn = function(n, src, got, all) {
if (n == 0) {
if (got.length > 0) {
all[all.length] = got;
}
return;
}
for (var j = 0; j < src.length; j++) {
fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
}
return;
}
let all = [];
for (i = min; i < a.length; i++) {
fn(i, a, [], all);
}
all.push(a);
return all;
}
const examplePhone = [6, 0, 2, 7, 6, 9, 3, 6, 8, 9];
function getCombos(str) {
let result = [];
str.forEach((x) => {
const phonepadArray = phonepad[x];
return phonepadArray.reduce((acc, cv, ci, ar) => {
const res = combine(ar, 2);
return result.push(res);
}, 0)
})
return result;
}
console.log('subsets', getCombos(examplePhone));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment