生成序列A到Z然后0到9,从A到999

Generate Sequence A to Z and then 0 to 9, from A to 999

在此先感谢,我想生成从 A 到 Z 然后是 0 到 9 的序列,然后它将移动到 AA、AB、AC ..... AZ、A0、A1 .... A9、BA等

我曾尝试按以下方式实现它

public static string GenerateSequence(List<string> inputList)
    {
        string identifierCode = "A";
        //check if list does not contains any element
        if (!inputList.Any()) return identifierCode;
        //sort codes
        inputList.Sort();
        //get last code
        var lastItem = inputList[inputList.Count - 1];
        //split code
        var splittedChars = lastItem.ToCharArray();
        bool incrementNext = true;
        for (int i = 0; i < splittedChars.Length; i++)
        {
            if (incrementNext)
            {
                var effectedNumber = splittedChars.Length - (i + 1);
                if (effectedNumber >= 0)
                {
                    var charToIncrement = splittedChars[effectedNumber];
                    switch (charToIncrement)
                    {
                        case 'Z':
                            charToIncrement = '0';
                            incrementNext = false;
                            break;
                        case '9':
                            charToIncrement = 'A';
                            incrementNext = true;
                            splittedChars[effectedNumber] = charToIncrement;
                            break;
                        default:
                            charToIncrement++;
                            incrementNext = false;
                            break;
                    }
                    splittedChars[effectedNumber] = charToIncrement;
                }
                else
                {
                    return "A" + splittedChars;
                }
            }
        }

        return new string(splittedChars);
    }

但是 inputList.Sort() 在字母表之前对数字进行排序,所以我的代码在 Z

之后失败

伪代码:

Base enumeration: 
   yield return A, B, C .... 8, 9;

Next enumeration:
   for each item in base enumeration
       for each item2 in base enumeration
           yield return item + item2

Enumeration N:
    for each item in base enumeration
       for each item2 in N-1 enumeration
           yield return item + item2

那么我们该怎么做呢?这是递归函数的典型例子:

  1. 有一个易于识别的基本情况:基本枚举。
  2. N深度枚举建立在N减一深度枚举之上

考虑到这一点,请考虑以下代码:

public static IEnumerable<string> GetNthEnumeration(IEnumerable<string> baseEnumeration, int n)
{
    if (baseEnumeration == null) throw new ArgumentNullException();

    if (n < 0) throw new ArgumentOutOfRangeException();

    if (n == 0) //base case
    {
        foreach (var item in baseEnumeration) { yield return item; }
    }
    else //build recursively
    {
        foreach (var pre in baseEnumeration) 
        {
            foreach (var post in GetNthEnumeration(baseEnumeration, n - 1))
            {
                yield return pre + post; 
            }
        }
    }
 }

您有以下问题导致 "a" 大于 "A" 且 "A" 大于“0”:请参阅以下 ascii table .However, you can use your own comparator IComparer

此外,您可以通过以下方法进行测试:

    public static string GetExcelColumnName(int index)
    {
        var alphabet = new char[]
        {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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 name = new char[3];
        var rem = index;

        for (var i = 2; i >= 0; i--)
        {
            var tmp = rem % alphabet.Length;
            var r = alphabet[tmp];
            name[i] = r;
            rem = (rem-tmp) / alphabet.Length;
        }

        return new string(name);
    }

生成所需序列的递归方法如下

        public static string GenerateSequence(int col)
    {
        if (col>=1 && col <= 36)
        {  
            string schars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            return schars[col-1].ToString();
        }
        int div = col / 36;
        int mod = col % 36;
        if (mod == 0) { mod = 36; div--; }
        return GenerateSequence(div) + GenerateSequence(mod);
    }


    static void Main(string[] args)
    {

        for (int i = 1; i < 250; i++)
        {
            Console.WriteLine(i + "---" + GenerateSequence(i));
        }

    }