Skip to content

Instantly share code, notes, and snippets.

@geoffb geoffb/permut.js
Created Feb 24, 2017

Embed
What would you like to do?
Calculate permutations
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
You can’t perform that action at this time.