Created
February 24, 2017 03:59
-
-
Save geoffb/4bb940c9d0fc430c93a8d6389a367ac4 to your computer and use it in GitHub Desktop.
Calculate permutations
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
var permut = function (array, count, initial, output) { | |
if (initial.length >= count) { | |
output.push(initial); | |
} else { | |
for (var i = 0; i < array.length; ++i) { | |
permut(array, count, initial.concat(array[i]), output); | |
} | |
} | |
}; | |
var getPermutations = function (array, count) { | |
var output = []; | |
permut(array, count, [], output); | |
return output; | |
}; | |
var PART_VALUES = [0, 1, 2, 3, 4]; // 0 = Rabbit, 1 = Deer, etc | |
var PART_COUNT = 5; // Number of "slots", head, eyes, feet, etc | |
var perms = getPermutations(PART_VALUES, PART_COUNT); | |
console.log(perms.length + " permutations"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment