Created
December 15, 2014 12:44
-
-
Save cocodrips/62e51dae5af48892602c to your computer and use it in GitHub Desktop.
combination-js
This file contains hidden or 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
n_combination: (sets, n) -> | |
if sets.length < n or n <= 0 | |
return [] | |
if sets.length == n | |
return [sets] | |
if n == 1 | |
return (c for c in sets) | |
combs = [] | |
for i in [0...sets.length - n + 1] | |
head = sets.slice(i, i + 1); | |
tailcombs = n_combination(sets.slice(i+1), n - 1) | |
for j in [0...tailcombs.length] | |
combs.push(head.concat(tailcombs[j])) | |
return combs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment