当前位置:  开发笔记 > 编程语言 > 正文

找出给定长度的所有可能单词的好方法是什么

如何解决《找出给定长度的所有可能单词的好方法是什么》经验,为你挑选了3个好方法。

我正在尝试在C#中创建一个算法,它产生以下输出字符串:

AAAA
AAAB
AAAC
...and so on...
ZZZX
ZZZY
ZZZZ

完成此任务的最佳方法是什么?

public static IEnumerable GetWords()
{
    //Perform algorithm
    yield return word;
}

angry person.. 17

好吧,如果长度是一个常数4,那么这将处理它:

public static IEnumerable GetWords()
{
    for (Char c1 = 'A'; c1 <= 'Z'; c1++)
    {
        for (Char c2 = 'A'; c2 <= 'Z'; c2++)
        {
            for (Char c3 = 'A'; c3 <= 'Z'; c3++)
            {
                for (Char c4 = 'A'; c4 <= 'Z'; c4++)
                {
                    yield return "" + c1 + c2 + c3 + c4;
                }
            }
        }
    }
}

如果长度是一个参数,这个递归解决方案将处理它:

public static IEnumerable GetWords(Int32 length)
{
    if (length <= 0)
        yield break;

    for (Char c = 'A'; c <= 'Z'; c++)
    {
        if (length > 1)
        {
            foreach (String restWord in GetWords(length - 1))
                yield return c + restWord;
        }
        else
            yield return "" + c;
    }
}


Garry Shutle.. 15

总是有强制性的LINQ实现.最有可能是垃圾性能,但是从什么时候开始使用酷炫的新功能呢?

var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

var sequence = from one in letters
               from two in letters
               from three in letters
               from four in letters
               orderby one, two, three, four
               select new string(new[] { one, two, three, four });

'sequence'现在是一个包含AAAA到ZZZZ的IQueryable.

编辑:

好的,所以我觉得应该可以通过LINQ使用可配置的字母表来创建一个可配置长度序列.所以这就是.再一次,完全没有意义,但这让我烦恼.

public void Nonsense()
{
    var letters = new[]{"A","B","C","D","E","F",
                        "G","H","I","J","K","L",
                        "M","N","O","P","Q","R","S",
                        "T","U","V","W","X","Y","Z"};

    foreach (var val in Sequence(letters, 4))
        Console.WriteLine(val);
}

private IQueryable Sequence(string[] alphabet, int size)
{
    // create the first level
    var sequence = alphabet.AsQueryable();

    // add each subsequent level
    for (var i = 1; i < size; i++)
        sequence = AddLevel(sequence, alphabet);

    return from value in sequence
           orderby value
           select value;
}

private IQueryable AddLevel(IQueryable current, string[] characters)
{
    return from one in current
           from character in characters
           select one + character;
}

对Sequence方法的调用产生与之前相同的AAAA到ZZZZ列表,但现在您可以更改使用的字典以及生成的字词的长度.



1> angry person..:

好吧,如果长度是一个常数4,那么这将处理它:

public static IEnumerable GetWords()
{
    for (Char c1 = 'A'; c1 <= 'Z'; c1++)
    {
        for (Char c2 = 'A'; c2 <= 'Z'; c2++)
        {
            for (Char c3 = 'A'; c3 <= 'Z'; c3++)
            {
                for (Char c4 = 'A'; c4 <= 'Z'; c4++)
                {
                    yield return "" + c1 + c2 + c3 + c4;
                }
            }
        }
    }
}

如果长度是一个参数,这个递归解决方案将处理它:

public static IEnumerable GetWords(Int32 length)
{
    if (length <= 0)
        yield break;

    for (Char c = 'A'; c <= 'Z'; c++)
    {
        if (length > 1)
        {
            foreach (String restWord in GetWords(length - 1))
                yield return c + restWord;
        }
        else
            yield return "" + c;
    }
}



2> Garry Shutle..:

总是有强制性的LINQ实现.最有可能是垃圾性能,但是从什么时候开始使用酷炫的新功能呢?

var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

var sequence = from one in letters
               from two in letters
               from three in letters
               from four in letters
               orderby one, two, three, four
               select new string(new[] { one, two, three, four });

'sequence'现在是一个包含AAAA到ZZZZ的IQueryable.

编辑:

好的,所以我觉得应该可以通过LINQ使用可配置的字母表来创建一个可配置长度序列.所以这就是.再一次,完全没有意义,但这让我烦恼.

public void Nonsense()
{
    var letters = new[]{"A","B","C","D","E","F",
                        "G","H","I","J","K","L",
                        "M","N","O","P","Q","R","S",
                        "T","U","V","W","X","Y","Z"};

    foreach (var val in Sequence(letters, 4))
        Console.WriteLine(val);
}

private IQueryable Sequence(string[] alphabet, int size)
{
    // create the first level
    var sequence = alphabet.AsQueryable();

    // add each subsequent level
    for (var i = 1; i < size; i++)
        sequence = AddLevel(sequence, alphabet);

    return from value in sequence
           orderby value
           select value;
}

private IQueryable AddLevel(IQueryable current, string[] characters)
{
    return from one in current
           from character in characters
           select one + character;
}

对Sequence方法的调用产生与之前相同的AAAA到ZZZZ列表,但现在您可以更改使用的字典以及生成的字词的长度.



3> Olmo..:

只是对Garry Shutler的讽刺,但我想要代码着色:

你真的不需要使它成为IQuaryable,也不需要排序,所以你可以删除第二种方法.一步转发是使用Aggregate进行交叉产品,它最终会像这样:

IEnumerable letters = new[]{
                "A","B","C","D","E","F",                       
                "G","H","I","J","K","L",
                "M","N","O","P","Q","R","S",           
                "T","U","V","W","X","Y","Z"};

var result = Enumerable.Range(0, 4)
                .Aggregate(letters, (curr, i) => curr.SelectMany(s => letters, (s, c) => s + c));

foreach (var val in result)
     Console.WriteLine(val);

安德斯应该获得Linq的诺贝尔奖!

推荐阅读
echo7111436
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有