c# - 加权排列算法

标签 c# .net algorithm

团队:

我正在构建一个上下文感知的业务规则引擎——但它是加权的——或者换句话说,每个业务规则都有一个由键的段定义的粒度级别。这些段不是组合的,因为它们不能按任何顺序加权,而是像组合锁一样可置换(有趣的是命名不当但被广泛接受)。

但是,为了减少提供业务规则所需的代码量,我们只构建了排除文件,这意味着每个段都可以以特定键值或 ALL 结束。

那么,现在我们有了一个抽象的背景,让我们来看一个具体的例子。定义的段如下:

  1. 业务线 (LOB)
  2. 公司
  3. 国家

现在,我们假设 LOB 是 ABC,公司是 G,州是 WY。如果你把它分解,我应该得到以下排列:

  • ABC_G_WY
  • ABC_G_ALL
  • ABC_ALL_WY
  • ABC_ALL_ALL
  • ALL_G_WY
  • ALL_G_ALL
  • ALL_ALL_WY
  • ALL_ALL_ALL

但是,我需要一个算法来解决这个问题。这些段也必须按照上述顺序返回,因为您必须始终首先找到最有限的规则

我期待着您的回复,并提前感谢大家!

最佳答案

public static void Main(string[] args)
{
    List<string> inputValues = new List<string>() { "ABC", "G", "WY" };
    List<string> results = new List<string>();

    int permutations = (int)Math.Pow(2.0, (double)inputValues.Count);

    for (int i = 0; i < permutations; i++)
    {
        int mask = 1;
        Stack<string> lineValues = new Stack<string>();
        for (int j = inputValues.Count-1; j >= 0; j--, mask <<= 1)
        {
            if ((i & mask) == 0)
            {
                lineValues.Push(inputValues[j]);
            }
            else
            {
                lineValues.Push("ALL");
            }
        }
        results.Add(string.Join("_", lineValues.ToArray())); //ToArray can go away in 4.0(?) I've been told.  I'm still on 3.5
    }

    foreach (string s in results)
    {
        Console.WriteLine(s);
    }

    Console.WriteLine("Press any key to exit...");
    Console.ReadKey(true);
}

关于c# - 加权排列算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9179736/

相关文章:

c# - 构建docker镜像的问题,RUN dotnet restore : "No such host is known" in VS2019, Windows

c# - 在 MonoTouch 中的编译时检测 JIT

asp.net - 图像作为 Asp.Net 5 类库中的资源

.net - WinForms:存储在 .resx 文件中的值会发生什么变化?

algorithm - 计算没有连续元素的子集总数

c# - 检索包含混合内容的完整 XElement 字符串

c# - AsParallel 扩展的实际工作原理

c# - 使用 datetime.addhours(-1) 时更新日期时间对象的日期

algorithm - 查找数组中最接近的整除数

algorithm - 如何找到任何规模的井字游戏的赢家?