Created
April 20, 2016 16:01
-
-
Save enyim/fec88ed255830dd631aa1ed7d1d5f625 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
| static IEnumerable<string> Permutate<T>(T[][] parts) | |
| { | |
| var counters = new int[parts.Length]; | |
| var max = parts.Aggregate(1, (a, p) => a * p.Length); | |
| if (max < 0) throw new ArgumentOutOfRangeException("Underflow; too many parts"); | |
| var counterPos = parts.Length - 1; | |
| var count = 0; | |
| while (count < max) | |
| { | |
| var pwd = String.Join("", counters.Select((i, j) => parts[j][i])); | |
| yield return pwd; | |
| for (var i = parts.Length - 1; i >= 0; i--) | |
| { | |
| counters[i]++; | |
| if (counters[i] == parts[i].Length) | |
| counters[i] = 0; | |
| else break; | |
| } | |
| count++; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: do not calculate "max"