A recursive solution, originally seen here seen here, but modified to fit your requirements (and look a little more JavaScript-y):
function combinations(str) {
var fn = function(active, rest, a) {
if (!active && !rest)
return;
if (!rest) {
a.push(active);
} else {
fn(active + rest[0], rest.slice(1), a);
fn(active, rest.slice(1), a);
}
return a;
}
return fn("", str, []);
}
Test:
combinations("abcd")
Output:
["abcd", "abc", "abd", "ab", "acd", "ac", "ad", "a", "bcd", "bc", "bd", "b", "cd", "c", "d"]
Regarding the name: Don't name it permutations
; a permutation is an arrangement of all the original elements (of which there should be n!
total). In other words, it already has a precise meaning; don't unnecessarily overload it. Why not simply name it combinations
?
A recursive solution, originally seen here, but modified to fit your requirements (and look a little more JavaScript-y):
function combinations(str) {
var fn = function(active, rest, a) {
if (!active && !rest)
return;
if (!rest) {
a.push(active);
} else {
fn(active + rest[0], rest.slice(1), a);
fn(active, rest.slice(1), a);
}
return a;
}
return fn("", str, []);
}
Test:
combinations("abcd")
Output:
["abcd", "abc", "abd", "ab", "acd", "ac", "ad", "a", "bcd", "bc", "bd", "b", "cd", "c", "d"]
Regarding the name: Don't name it permutations
; a permutation is an arrangement of all the original elements (of which there should be n!
total). In other words, it already has a precise meaning; don't unnecessarily overload it. Why not simply name it combinations
?
A recursive solution, originally seen here, but modified to fit your requirements (and look a little more JavaScript-y):
function combinations(str) {
var fn = function(active, rest, a) {
if (!active && !rest)
return;
if (!rest) {
a.push(active);
} else {
fn(active + rest[0], rest.slice(1), a);
fn(active, rest.slice(1), a);
}
return a;
}
return fn("", str, []);
}
Test:
combinations("abcd")
Output:
["abcd", "abc", "abd", "ab", "acd", "ac", "ad", "a", "bcd", "bc", "bd", "b", "cd", "c", "d"]
Regarding the name: Don't name it permutations
; a permutation is an arrangement of all the original elements (of which there should be n!
total). In other words, it already has a precise meaning; don't unnecessarily overload it. Why not simply name it combinations
?
Regarding the nameA recursive solution, originally seen here , but modified to fit your requirements (and look a little more JavaScript-y): don't
function combinations(str) {
var fn = function(active, rest, a) {
if (!active && !rest)
return;
if (!rest) {
a.push(active);
} else {
fn(active + rest[0], rest.slice(1), a);
fn(active, rest.slice(1), a);
}
return a;
}
return fn("", str, []);
}
Test:
combinations("abcd")
Output:
["abcd", "abc", "abd", "ab", "acd", "ac", "ad", "a", "bcd", "bc", "bd", "b", "cd", "c", "d"]
Regarding the name: Don't name it permutations
; it implies that orderinga permutation is significantan arrangement of all the original elements (and I'd expect the result to containof which there should be n!
itemstotal). In other words, it already has a precise meaning; don't unnecessarily overload it. Why not simply name it combinations
?
Regarding the name: don't name it permutations
; it implies that ordering is significant (and I'd expect the result to contain n!
items). Why not simply name it combinations
?
A recursive solution, originally seen here , but modified to fit your requirements (and look a little more JavaScript-y):
function combinations(str) {
var fn = function(active, rest, a) {
if (!active && !rest)
return;
if (!rest) {
a.push(active);
} else {
fn(active + rest[0], rest.slice(1), a);
fn(active, rest.slice(1), a);
}
return a;
}
return fn("", str, []);
}
Test:
combinations("abcd")
Output:
["abcd", "abc", "abd", "ab", "acd", "ac", "ad", "a", "bcd", "bc", "bd", "b", "cd", "c", "d"]
Regarding the name: Don't name it permutations
; a permutation is an arrangement of all the original elements (of which there should be n!
total). In other words, it already has a precise meaning; don't unnecessarily overload it. Why not simply name it combinations
?