Skip to content

Instantly share code, notes, and snippets.

@holyshared
Last active March 13, 2021 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holyshared/10ddfaa82efa434f811ac1d5d1767fb3 to your computer and use it in GitHub Desktop.
Save holyshared/10ddfaa82efa434f811ac1d5d1767fb3 to your computer and use it in GitHub Desktop.
Pattern
const items = [1, 2, 3];
function pattern(items, paths, results) {
if (items.length === 0) {
results.push([ ...paths ]);
return results
}
let curr = results;
for (let i = 0; i < items.length; i++) {
paths.push(items[i]);
curr = pattern(items.filter(item => item != items[i]), paths, curr);
paths.pop();
}
return curr;
}
function allPattern(items) {
return pattern(items, [], []);
}
const results = allPattern(items);
console.log(results);
console.log(results.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment