C# 用不同的字符集生成所有可能的组合

C# Generate all possible combination with different charsets

我想创建一个生成器,生成 N 个长度和 N 个不同字符集的所有可能组合。

示例:输入字符串 -> AAA000 -> 输出 -> aaa000、aaa001 ... aaz999、aba000、aba001 ... zzz999。

在此示例中,我想生成(而不是每个 A)字母字符集组合和(而不是每个 0)数字字符集组合。所以输出取决于输入字符串。

我是 C# 的初学者,我只创建了具有单一字符集的简单生成器。但是对我没用。

感谢您的任何想法。

您可以使用 Eric Lippert's code to produce combinations 来实现它。

这是一个演示。您要调用的方法是 Combinations() - 它根据您的要求接受模式并输出一系列组合:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string pattern = "AA00";

            foreach (var s in Combinations(pattern))
                Console.WriteLine(s);
        }

        public static IEnumerable<string> Combinations(string pattern)
        {
            string letters = "abcdefghijklmnopqrstuvwxyz";
            string digits  = "0123456789";

            var sets = pattern.Select(ch => ch == 'A' ? letters : digits);

            return Combine(sets).Select(x => new String(x.ToArray()));
        }

        public static IEnumerable<IEnumerable<T>> Combine<T>(IEnumerable<IEnumerable<T>> sequences)
        {
            IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };

            return sequences.Aggregate(
              emptyProduct,
              (accumulator, sequence) =>
                from accseq in accumulator
                from item in sequence
                select accseq.Concat(new[] { item }));
        }
    }
}

注意:为了简洁起见,我省略了所有参数检查和验证。


[编辑] 下面是扩展示例以显示如何添加其他字符集:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            string pattern = "A0*ë";

            foreach (var s in Combinations(pattern))
                Console.WriteLine(s);
        }

        public static IEnumerable<string> Combinations(string pattern)
        {
            var sets = pattern.Select(charset);

            return Combine(sets).Select(x => new String(x.ToArray()));
        }

        private static string charset(char charsetCode)
        {
            switch (charsetCode)
            {
                case 'A': return "abcdefghijklmnopqrstuvwxyz";
                case '0': return "0123456789";
                case '*': return "!£$%^&*()_+=-";
                case 'ë': return "àáâãäåæçèéêë";

                // Add new charset codes and charsets here as desired.

                default:  throw new InvalidOperationException("Bad charset code: " + charsetCode);
            }
        }

        public static IEnumerable<IEnumerable<T>> Combine<T>(IEnumerable<IEnumerable<T>> sequences)
        {
            IEnumerable<IEnumerable<T>> emptyProduct = new[] {Enumerable.Empty<T>()};

            return sequences.Aggregate(
                emptyProduct,
                (accumulator, sequence) =>
                    from accseq in accumulator
                    from item in sequence
                    select accseq.Concat(new[] {item}));
        }
    }
}