简体   繁体   English

如何生成所有可能的单词

[英]How to generate all possible words

I'm new to programming (C#).我是编程新手(C#)。

The application is a 'words generator'.该应用程序是一个“单词生成器”。

What I'm looking for is a for loop that can generate all possible words with the characters in a given array of characters.我正在寻找的是一个 for 循环,它可以使用给定字符数组中的字符生成所有可能的单词。

The details:细节:

I have a List<char> = { A,a,6,w,# } (The length may vary)我有一个List<char> = { A,a,6,w,# } (长度可能会有所不同)

I want to generate all possible words (for example: 4 letters length ) with this character set.我想用这个字符集生成所有可能的单词(例如:4 个字母长度)。 This options should generate 5 characters & 4 letters = 5*5*5*5 = 625 words .此选项应生成5 characters & 4 letters = 5*5*5*5 = 625 words

All generated words should be every possible combination of the given letters only

NOTE: Some might tell me that i should use a solution called ( Permutations of a String/Integer ) this method seems to be fine if the required words length is same as given characters length, but in my case i might give the application 100 characters, But i want it to generate all possible words -> 4 letters length (Example: MaRk, M@rK,m4rK...)注意:有些人可能会告诉我,我应该使用名为( 字符串/整数的排列)的解决方案如果所需的单词长度与给定的字符长度相同,则此方法似乎没问题,但在我的情况下,我可能会给应用程序 100 个字符, 但我希望它生成所有可能的单词 -> 4 个字母长度(例如:MaRk、M@rK、m4rK...)

You could use an IEnumerable<String> method: 您可以使用IEnumerable<String>方法:

public IEnumerable<String> GenerateStrings (IEnumerable<char> characters, int length) {
    if(length > 0) {
        foreach(char c in characters) {
            foreach(String suffix in GenerateStrings(characters,length-1)) {
                yield return c+suffix;
            }
        }
    } else {
        yield return string.Empty;
    }
}

Result with csharp (interactive C# shell): 使用csharp结果(交互式C#shell):

csharp> Foo.GenerateStrings(new char[] {'A','a','6','w','#'},3)
{ "AAA", "AAa", "AA6", "AAw", "AA#", "AaA", "Aaa", "Aa6", "Aaw", "Aa#", "A6A", "A6a", "A66", "A6w", "A6#", "AwA", "Awa", "Aw6", "Aww", "Aw#", "A#A", "A#a", "A#6", "A#w", "A##", "aAA", "aAa", "aA6", "aAw", "aA#", "aaA", "aaa", "aa6", "aaw", "aa#", "a6A", "a6a", "a66", "a6w", "a6#", "awA", "awa", "aw6", "aww", "aw#", "a#A", "a#a", "a#6", "a#w", "a##", "6AA", "6Aa", "6A6", "6Aw", "6A#", "6aA", "6aa", "6a6", "6aw", "6a#", "66A", "66a", "666", "66w", "66#", "6wA", "6wa", "6w6", "6ww", "6w#", "6#A", "6#a", "6#6", "6#w", "6##", "wAA", "wAa", "wA6", "wAw", "wA#", "waA", "waa", "wa6", "waw", "wa#", "w6A", "w6a", "w66", "w6w", "w6#", "wwA", "wwa", "ww6", "www", "ww#", "w#A", "w#a", "w#6", "w#w", "w##", "#AA", "#Aa", "#A6", "#Aw", "#A#", "#aA", "#aa", "#a6", "#aw", "#a#", "#6A", "#6a", "#66", "#6w", "#6#", "#wA", "#wa", "#w6", "#ww", "#w#", "##A", "##a", "##6", "##w", "###" }

The advantage of using a method with a yield statement is that it is lazy: if you only need five such strings, not all possible strings will be generated first... 将方法与yield语句一起使用的优点是它很懒:如果只需要五个这样的字符串,则不会首先生成所有可能的字符串。

Willem Van Onsem, thanks!威廉·范·翁塞姆,谢谢! This was exactly what i've been looking for.这正是我一直在寻找的。 But my problem sounds little different.但我的问题听起来没什么不同。 I have to generate all possible strings without repetition of chars from source array.我必须生成所有可能的字符串,而不需要从源数组中重复字符。 And here is your code, that i modified to do so:这是你的代码,我修改了它:

    public static IEnumerable<string> GenerateStrings(IEnumerable<char> characters, int length, int count)
    {
        if (length > 0)
        {
            foreach (char c in characters)
            {
                char[] charactersDec = new char[characters.Count()];
                Array.Copy(characters.ToArray(), charactersDec, characters.Count());
                int index = Array.IndexOf(charactersDec, c);
                charactersDec = charactersDec.Where((val, idx) => idx != index).ToArray();
                foreach (string suffix in GenerateStrings(charactersDec, length - 1, count++))
                {
                    yield return c + suffix;
                }
            }
        }
        else
        {
            yield return string.Empty;
        }
    }

I remove current char from array and passed this array to recursive call.我从数组中删除当前字符并将这个数组传递给递归调用。

output for a, b, c, d will be: ab ba ac ca ad da bc cb bd db cd dc please, sorry my english. a、b、c、d 的输出将是:ab ba ac ca ad da bc cb bd db cd dc 请,对不起我的英语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM