-3

I want to make a program to find all valid stock symbols from Yahoo Finance and I already found this: Quickest way to enumerate the alphabet However, I wan't it to go from A - Z and then AA - AZ and then ABA - ABZ and so on and so forth. What is the best way to do this? More clear example: A B C D ect. AA AB AC AD ect. ABA ABB ABC ABD ect.

Community
  • 1
  • 1
Alerik
  • 25
  • 5
  • Take a look at [this](http://stackoverflow.com/questions/32719912/dynamic-character-generator-generate-all-possible-strings-from-a-character-set) – Yacoub Massad Sep 30 '15 at 22:02

2 Answers2

0

Not sure how fast it is, but I did the following when I needed to do something similar:

        for (int i = 0; i < numCols && i < 26; i++)
        {
            char start = 'A';
            char colChar = (char)(start + (char)(i));
            Console.WriteLine(string.Format("{0}", colChar), typeof(string));
        }
        for (int i = 26; i < 52 && i < numCols; i++)
        {
            char start = 'A';
            char colChar = (char)(start + (char)(i-26));
            Console.WriteLine(string.Format("A{0}", colChar), typeof(string));
        }

the second for loop obviously only returns AA thru AZ, but if you put that in a function, made the first A as an input, then you could loop through A-Z for the first character, and you have all the two character results. Creating a third function with a 2 character input as the preceeding string will get you the three character set.

26*26*26 is a lot of output, but the above pattern should get it for you.

Owen Ivory
  • 244
  • 1
  • 9
0

Using Eric Lippert's Cartesian Product,

var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int maxlen = 3;

var query = Enumerable.Range(1, maxlen)
            .SelectMany(i => Enumerable.Repeat(chars, i)
                             .CartesianProduct()
                             .Select(x => String.Concat(x)));


foreach(var str in query)
{
    Console.WriteLine(str);
}

PS: Just for the completeness:

public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
{
    // base case:
    IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
    foreach (var sequence in sequences)
    {
        var s = sequence; // don't close over the loop variable
                            // recursive case: use SelectMany to build the new product out of the old one
        result =
            from seq in result
            from item in s
            select seq.Concat(new[] { item });
    }
    return result;
}

Eser
  • 12,346
  • 1
  • 22
  • 32