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

此生再无相见时 提交于 2019-12-11 04:17:41

问题


Thanks in advance, I want to generate sequence from A to Z and after that 0 to 9 and after that it will move to AA, AB, AC ..... AZ, A0, A1 .... A9, BA and so on

i had tried to implement it as following

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

but inputList.Sort() sorts numbers before Alphabets so my code fails after Z


回答1:


A recursive approach to generate sequence required is as follows

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

    }



回答2:


Pseudo code:

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

So how do we do this? This is the canonical example of a recursive function:

  1. There is an easily identifiable base case: the base enumeration.
  2. The N deep enumeration is built on the N minus one deep enumeration.

With that in mind, consider the following code:

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



回答3:


You have the following issue cause "a" is greater than "A" and "A" is greater than "0": see the following ascii table .However, you can use your own comparator IComparer

Moreover, you can test the following method:

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


来源:https://stackoverflow.com/questions/41078549/generate-sequence-a-to-z-and-then-0-to-9-from-a-to-999

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!