Number of loop recursions as a parameter

c#

Solution

How about a method like this:

public IEnumerable<string> SomeMethod(int NumberOfChars) 
{
    if (NumberOfChars == 0)
    {
        yield return string.Empty;
    }
    else 
    {
        for (var i = 'a'; i <= 'z'; i++)
        {
            foreach (var s in SomeMethod(NumberOfChars - 1)) 
            {
                yield return i + s;
            }
        }
    }
}

And just for fun, here's another solution using Linq:

public IEnumerable<string> SomeMethod(int n) 
{
    var r = Enumerable.Range('a', 26).Select(x => ((char)x).ToString());
    return (n > 1) ? r.SelectMany(x => SomeMethod(n - 1), (x, y) => x + y) : r;
}

Problem

I'm building a list of strings that contains all permutations of 2-letter strings, for instance "aa" to "zz". This is what I have: ``` public List<string> SomeMethod(int NumberOfChars) { for (var i = 0; i < 26; i++) { char character1 = (char)(i + 97); var Letter1 = character1.ToString(); for (var j = 0; j < 26; j++) { char character2 = (char)(j + 97); var Letter2 = character2.ToString(); string TheString = Letter1 + Letter2; TheList.Add(TheString); } } return TheList; } ``` Basically, it's a loop inside a loop that combines characters from the alphabet. Now suppose I want to include `NumberOfChars` as a parameter that determines the length of each string. For instance, if I pass in 2 it would return all 676 2-letter strings from `"aa"` to `"zz"` and if I pass in 3 it would return all 17,576 3-letter strings from `"aaa"` to `"zzz"`. For the moment, the easiest way to do it would be to have two different methods, one that returns 2-letter strings with two nested loops and another one that returns 3-letter strings with 3 nested loops. What's a cleaner way to do this? Thanks.

Original source