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

时间:2016-12-10 17:59:30

标签: c# sequence-generators

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

我曾尝试将其实施为以下

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()在Alphabets之前对数字进行排序,因此我的代码在Z

之后失败

3 个答案:

答案 0 :(得分:2)

伪代码:

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减去一个深枚举上。
  3. 考虑到这一点,请考虑以下代码:

    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; 
                }
            }
        }
     }
    

答案 1 :(得分:1)

您有以下问题原因&#34; a&#34;大于&#34; A&#34;和&#34; A&#34;大于&#34; 0&#34;:请参阅以下ascii table。但是,您可以使用自己的比较器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);
    }

答案 2 :(得分:1)

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

        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));
        }

    }