Skip to content

Instantly share code, notes, and snippets.

@cocodrips
Created December 15, 2014 12:44
Show Gist options
  • Save cocodrips/62e51dae5af48892602c to your computer and use it in GitHub Desktop.
Save cocodrips/62e51dae5af48892602c to your computer and use it in GitHub Desktop.
combination-js
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