GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — ToyProblems/permute.js at master · patrickford/ToyProblems Expand file tree/ Copy pathpermute.jsMore file actions 32 lines (31 loc) · 1.22 KB / Copy pathpermute.jsFile metadata and controls32 lines (31 loc) · 1.22 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 //result: Global array which holds the list of permutations //usedChars: Global utility array which holds a list of "currently-in-use" characters function permutations(str) { var result = [], usedChars = []; function permute(str) { //convert str into a char array (one element for each character) var ch; var chars = str.split(""); for (var i = 0; i < chars.length; i++) { //get and remove character at index "i" from char array ch = chars.splice(i, 1); //add removed character to the end of used characters usedChars.push(ch); //when there are no more characters left in char array to add, add used chars to list of permutations if (chars.length == 0) { // result[result.length] = usedChars.join(""); if (result.indexOf(usedChars.join("")) == -1) { result.push(usedChars.join("")); } } //send characters (minus the removed one from above) from char array to be permuted permute(chars.join("")); //add removed character back into char array in original position chars.splice(i, 0, ch); //remove the last character used off the end of used characters array usedChars.pop(); } } permute(str); return result; } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |