【问题标题】:Generate Sequence A to Z and then 0 to 9, from A to 999生成序列 A 到 Z,然后生成 0 到 9,从 A 到 999
【发布时间】:2016-12-11 07:44:13
【问题描述】:

提前谢谢,我想生成从 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 之后失败

【问题讨论】:

  • 如果您是初学者,最简单的方法是创建一个包含所有项目的数组。因此,在您的情况下,它将是[“A”,“B”...“Z”,“0”,“1”...“9”]。老实说:如果从那时起你不能很容易地做到这一点,你需要一个新的爱好!从最基本的编程概念重新开始。
  • @JoeBlow:字符是序数,所以 Enumerable.Range() 是你真正需要的...
  • 是的,我只是在想最简单的方法。很少有爱好者知道“可枚举”是什么意思!
  • 感谢您给我宝贵的时间,实际上它的计数器在我的情况下必须以数百万运行,看起来很讨厌,但根据用户要求我必须忍受它,我尝试了一些其他技术但是还是没有这么幸运.....

标签: c# sequence-generators


【解决方案1】:

伪代码:

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

【讨论】:

  • 一个简单算法的优秀解释。不幸的是,它完全符合规定的要求,以 36 减 1(“999”)的幂结尾。要在较低的端点结束,将产生超越的力量,然后是 Take 或 TakeWhile。
【解决方案2】:

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

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

    }

【讨论】:

  • 谢谢,你成就了我的一天,这正是我所需要的,整洁、干净、简洁、中肯
【解决方案3】:

您有以下问题,因为“a”大于“A”并且“A”大于“0”:请参阅以下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);
    }

【讨论】:

    猜你喜欢
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多