Created
August 12, 2011 03:58
-
-
Save Gurpartap/1141421 to your computer and use it in GitHub Desktop.
Revisions
-
Gurpartap revised this gist
Aug 12, 2011 . 1 changed file with 48 additions and 35 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,41 +1,54 @@ var symbols = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split(''); var max_length = symbols.length; for (var length = 1; length <= max_length; length++) { var scombos = combinations(symbols, length); for (var j = 0; j < scombos.length; j++) { var word = scombos[j]; console.log(word.join('')); } } function combinations(superset, size) { var result = []; if (superset.length < size) {return result;} var done = false; var current_combo, distance_back, new_last_index; var indexes = []; var indexes_last = size - 1; var superset_last = superset.length - 1; // initialize indexes to start with leftmost combo for (var i = 0; i < size; ++i) { indexes[i] = i; } while (!done) { current_combo = []; for (i = 0; i < size; ++i) { current_combo.push(superset[indexes[i]]); } result.push(current_combo); if (indexes[indexes_last] == superset_last) { done = true; for (i = indexes_last - 1; i > -1 ; --i) { distance_back = indexes_last - i; new_last_index = indexes[indexes_last - distance_back] + distance_back + 1; if (new_last_index <= superset_last) { indexes[indexes_last] = new_last_index; done = false; break; } } if (!done) { ++indexes[indexes_last - distance_back]; --distance_back; for (; distance_back; --distance_back) { indexes[indexes_last - distance_back] = indexes[indexes_last - distance_back - 1] + 1; } } } else {++indexes[indexes_last]} } return result; } -
Gurpartap created this gist
Aug 12, 2011 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ var table = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; String.prototype.replaceAt=function(index, char) { return this.substr(0, index) + char + this.substr(index+char.length); } function parseForNextID(currentID) { for (var i = currentID.length - 1; i >= 0; i--) { var indexOfChar = table.indexOf(currentID.charAt(i)) + 1; var charAtIndex = table.charAt(indexOfChar); if (charAtIndex != '') { var replaceWith = table.charAt(indexOfChar); currentID = currentID.replaceAt(i, replaceWith); console.log(currentID); return parseForNextID(currentID); } else if (i - 1 >= 0 && currentID.charAt(i - 1) != "Z") { var replaceWith = table.charAt(0); var indexOfChar = table.indexOf(currentID.charAt(i - 1)) + 1; var charAtIndex = table.charAt(indexOfChar); currentID = currentID.replaceAt(i, replaceWith); currentID = currentID.replaceAt(i - 1, charAtIndex); console.log(currentID); return parseForNextID(currentID); } } var nextID = table.charAt(0); for (var j = 0; j < currentID.length; j++) { nextID = nextID + table.charAt(0); } console.log(nextID); return parseForNextID(nextID); } parseForNextID("0");