繁体   English   中英

生成两个字母数字字符串范围之间的所有可能组合

[英]Generating every possible combination between two alphanumeric string ranges

我正在使用SAP Idocs,它的细分可以包含成本中心参考范围。 范围以字符串的开始和结束值形式给出。 要将这些值存储在数据库中,我需要生成这些值之间存在的所有可能组合。

字符串是字母数字,例如开始: D98C1和结束: D9AZ3 单个char序列首先是从0到9的数字,然后是从A到Z的字母。扩展需要生成开始和结束之间的所有可能组合,例如说开始: A1到结束: CA将包含值A1A9AAAZB0B9BABZC0C9CA

我完全停留在这一点上,并且非常感谢一些有关如何实现的指示。

编辑:

作为一个人,我首先要找到开始和结束字符串之间不同的部分。 我可以做到,那很容易。 因此,对于上面的示例,该D9 然后,我将一次遍历起始字符串的可变部分,并改变字符串末尾的所有字符,遍历所有可能的字符,直到到达末尾字符串中的相应字符。

我只是坚持执行。

我开始时是这样的:

readonly static char[] values = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToArray(); 

    static void Main(string[] args)
    {
        string from = "D3A0";

        string to = "D3AC";

        string root = new string(from.TakeWhile((c, i) => to.Length >= i && to[i] == c).ToArray());

        string from1 = from.Substring(root.Length);

        string to1 = to.Substring(root.Length);

        var output = new List<string>();

        for (int i = from1.Length - 1; i == 0; i--)
        {
            char startChar = from1[i];

            char endChar = to1[i];

            var remainingValues = values.SkipWhile(v => v != startChar)
                                        .TakeWhile(v => v != endChar)
                                        .ToList();

            foreach (char v in remainingValues)
            {
                string currentValue = from1.Remove(i) + v;

                output.Add(currentValue);
            }

            if (output.Contains(to1))
            {
                break;
            }
        }

        foreach (var s in output.Select(o => root + o))
        {
            Console.WriteLine(s);
        }
    }

但是它不能提供所有组合。

您在寻找什么叫做base36 因为它是一个以36个字母表示的数字。 与数字的任何其他表示形式一样,您可以将所有表示形式转换为数字,进行计算,然后再将其转换回显示:

using System;
using System.Linq;

namespace ConsoleApp11
{
    public static class Base36
    {
        private const string Digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        public static string ConvertToBase36(this int value)
        {
            string result = string.Empty;

            while (value > 0)
            {
                result = Digits[value % Digits.Length] + result; 
                value /= Digits.Length;
            }

            return result;
        }

        public static int ConvertFromBase36(this string value)
        {
            return value.Reverse().Select((character, index) => (int)Math.Pow(Digits.Length, index) * Digits.IndexOf(character)).Sum();
        }
    }

    class Program
    {
        static void Main()
        {
            var start = "D3A0";
            var end = "D3AC";

            var startNumber = start.ConvertFromBase36();
            var endNumber = end.ConvertFromBase36();

            while (startNumber < endNumber)
            {
                Console.WriteLine(startNumber.ConvertToBase36());
                startNumber++;
            }

            Console.ReadLine();
        }
    }
}

在此处输入图片说明

这样的事情怎么样:

public static string Next(string current)
{
    if (current.EndsWith("Z"))
        return Next(current.Substring(0, current.Length - 1)) + "0";
    if (current.EndsWith("9"))
        return current.Substring(0, current.Length - 1) + "A";
    return current.Substring(0, current.Length - 1) + (char) (current[current.Length-1] + 1);
}

然后这样称呼它

var value = "A1";
while (value != "CB")
{
    Console.WriteLine(value);
    value = Next(value);
}

也许是这样的:

private static string values = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public static void Main()
{
    string from = "Y5";

    string to = "ZZ";

    var counter = from;
    var output = new List<string>();
    while(counter != to) {
        counter = Increment(counter);
        output.Add(counter);
    }

    Console.WriteLine(string.Join(Environment.NewLine, output));
}

private static string Increment(string counter){
    if(counter.Length < 1) {
        counter = values[0] + counter;
    }

    var lastChar = counter[counter.Length - 1];
    var lastCharIndex = values.IndexOf(lastChar);

    if(lastCharIndex == -1)
    {
        throw new InvalidOperationException("Sequence contains an invalid character: " + lastChar);
    }

    var nextCharIndex = values.IndexOf(lastChar) + 1;

    if(nextCharIndex >= values.Length)
    {
        return Increment(counter.Substring(0, counter.Length - 1)) + values[0];
    }

    return counter.Substring(0, counter.Length - 1) + values[nextCharIndex];
}

应该与值的任何组合/顺​​序一起使用。

我首先做了以下准备-将值设置为可用的字符选项。 人类就是这样做的。 它效率不高,不光荣,但能胜任。

   private static readonly char[] values = { '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' };

    private static void Main(string[] args)
    {
        Console.Write("Start: ");
        //String start = Console.ReadLine();
        String start = "D98C1";
        Console.Write(" End: ");
        String end = "D9AZ3";
        //String end = Console.ReadLine();
        int i1 = Array.IndexOf(values, start[0]);
        int i2 = Array.IndexOf(values, start[1]);
        int i3 = Array.IndexOf(values, start[2]);
        int i4 = Array.IndexOf(values, start[3]);
        int i5 = Array.IndexOf(values, start[4]);


        while (String.Format("{0}{1}{2}{3}{4}", values[i1], values[i2], values[i3], values[i4], values[i5]) != end)
        {
            i5++;
            if (i5 == values.Length)
            {
                i5 = 0;
                i4++;
                if (i4 == values.Length)
                {
                    i4 = 0;
                    i3++;
                    if (i3 == values.Length)
                    {
                        i3 = 0;
                        i2++;

                        if (i2 == values.Length)
                        {
                            i2 = 0;
                            i1++;

                            if (i1 == values.Length)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            Console.WriteLine(String.Format("{0}{1}{2}{3}{4}", values[i1], values[i2], values[i3], values[i4], values[i5]));
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM