Last active
August 29, 2015 14:07
-
-
Save cdosborn/4fc91f05f0810132a1ff to your computer and use it in GitHub Desktop.
Recursion Spider Weasel
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
choose :: [a] -> Int -> [[a]] | |
choose _ 0 = [[]] | |
choose [] k = [] | |
choose (x:xs) k = thoseWith ++ thoseWithout | |
where | |
thoseWith = map (x :) (choose xs (k - 1)) | |
thoseWithout = choose xs k |
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
function combinations(numArr, choose, callback) { | |
var n = numArr.length; | |
var c = []; | |
var temp = []; | |
var inner = function(start, choose_) { | |
if (choose_ === 0) { | |
c.push(temp.slice()); | |
} else { | |
for (var i = start; i <= n - choose_; i++) { | |
temp.push(numArr[i]); | |
inner(i + 1, choose_ - 1); | |
temp.pop(); | |
} | |
} | |
} | |
inner(0, choose); | |
return c; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment