Created
January 16, 2013 09:10
-
-
Save zrbecker/4545748 to your computer and use it in GitHub Desktop.
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
| function range(start, stop, step) { | |
| if (typeof stop == 'undefined') { | |
| stop = start; | |
| start = 0; | |
| } | |
| if (typeof step == 'undefined') | |
| step = 1; | |
| if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) | |
| return []; | |
| var result = []; | |
| for (var i = start; step > 0 ? i < stop : i > stop; i += step) { | |
| result.push(i); | |
| } | |
| return result; | |
| } | |
| function Combinations(n, r) { | |
| this.n = n; | |
| this.r = r; | |
| this.indices = null; | |
| this.getNext = function() { | |
| if (r > n || r <= 0) | |
| return; | |
| if (this.indices == null) { | |
| this.indices = range(r); | |
| return this.indices; | |
| } else { | |
| var i = r - 1; | |
| for (; i >= 0; i -= 1) { | |
| if (this.indices[i] != n - r + i) | |
| break; | |
| if (i == 0) | |
| return; | |
| } | |
| this.indices[i] += 1; | |
| for (var j = i + 1; j < r; j += 1) | |
| this.indices[j] = this.indices[j - 1] + 1; | |
| return this.indices; | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment