Created
January 18, 2016 11:48
-
-
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
This file contains hidden or 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
| 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