Last active
March 13, 2021 07:11
-
-
Save holyshared/10ddfaa82efa434f811ac1d5d1767fb3 to your computer and use it in GitHub Desktop.
Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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