Skip to content

Instantly share code, notes, and snippets.

@roppa
Created January 18, 2016 11:48
Show Gist options
  • Select an option

  • Save roppa/eaa8cbabd36180bf2850 to your computer and use it in GitHub Desktop.

Select an option

Save roppa/eaa8cbabd36180bf2850 to your computer and use it in GitHub Desktop.
Seating permutations - take a list of people and it gives you all possible seating arrangements
let seating = (people) => {
let result = [];
(function permutations (left, right) {
let current;
let before
let after;
if (right.length <= 1) {
result.push(left.concat(right));
return;
}
for (let i = 0; i < right.length; i++) {
current = right.slice(i, i + 1);
before = right.slice(0, i);
after = right.slice(i + 1, right.length);
permutations(left.concat(current), before.concat(after));
}
}([], people));
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment