Created
February 26, 2015 23:51
-
-
Save jahands/fd7ecd4a5ac779fa515c to your computer and use it in GitHub Desktop.
Find all permutations with separate charset for each index
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace PasswordTableTest | |
{ | |
static class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Create a list of strings defining the charset for each index. | |
var charsets = new List<string> | |
{ "abcdef", "defghi", "ghijkl", "jklmno", "mnopqrstuvwxyz", "pq234r", "st2342u" }; | |
// Get the IEnumerable (note: This does not generate the permutations, just gives an IEnumerable to enumerate through thema ll) | |
var cp = CartesianProduct(charsets); | |
// Print them out | |
string row = ""; | |
foreach (string s in cp.Select(set => set.Aggregate("", (a, b) => a + b))) | |
{ | |
row += s + ' '; | |
if (row.Length > 60) | |
{ | |
Console.WriteLine(row); | |
row = ""; | |
} | |
} | |
Console.ReadKey(); | |
} | |
public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences) | |
{ | |
IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() }; | |
return sequences.Aggregate(result, (current, s) => | |
(from seq in current from item in s select seq.Concat(new[] {item}))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment