Skip to content

Instantly share code, notes, and snippets.

@jahands
Created February 26, 2015 23:51
Show Gist options
  • Save jahands/fd7ecd4a5ac779fa515c to your computer and use it in GitHub Desktop.
Save jahands/fd7ecd4a5ac779fa515c to your computer and use it in GitHub Desktop.
Find all permutations with separate charset for each index
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