基于模式/算法/公式动态地将不同的字符串添加到数组列表中

发布于 2024-09-18 00:41:26 字数 791 浏览 3 评论 0原文

有没有办法动态添加整个添加过程,而不是手动输入 a.Add("1 & 2"); 等值 顺便说一下,只有当我选择 5 个条件时才会发生这种情况 -我正在根据我满足的最大条件(即 5)进行添加,

下面显示的子集必须采用该模式,尽管顺序可以不同但值必须像 (1 & 2 & ) 一样递增。 3).所以不允许像 (1 & 4) 这样的跳跃值,也不允许像 (3 & 2) 这样的反向子集

ArrayList a = new ArrayList();
    a.Add("1 & 2");
    a.Add("2 & 3");
    a.Add("3 & 4");
    a.Add("4 & 5");
    a.Add("1 & 2 & 3");
    a.Add("2 & 3 & 4");
    a.Add("3 & 4 & 5");
    a.Add("1 & 2 & 3 & 4");
    a.Add("2 & 3 & 4 & 5");

所以如果我有 6 个条件,那么列表将添加 输入两个子集(5 和 6), 类型 3 子集(4 & 5 & 6), ...如果可能的话,甚至会创建另一种类型的子集,这将是 type 5(1 & 2 & 3 & 4 & 5) 在这种情况下

任何方法/方式都受欢迎,只要结果相同,即 arraylist 将包含基于最大条件的整个子集集。

干杯, 贾夫

is there a way to dynamically add this whole adding process instead of the manually entering the values like a.Add("1 & 2"); and so on
by the way, this only happens if i select 5 conditions
-i am doing the adding base on the maximum condition i cater which is 5

the subsets that i show below must be in that pattern although its ok that the order can be different but the values must be increment like (1 & 2 & 3).So jumping values like (1 & 4) is not allowed and no reverse subsets like (3 & 2)

ArrayList a = new ArrayList();
    a.Add("1 & 2");
    a.Add("2 & 3");
    a.Add("3 & 4");
    a.Add("4 & 5");
    a.Add("1 & 2 & 3");
    a.Add("2 & 3 & 4");
    a.Add("3 & 4 & 5");
    a.Add("1 & 2 & 3 & 4");
    a.Add("2 & 3 & 4 & 5");

So if i have 6 condition then the list will add on for
type two subsets(5 & 6),
type 3 subsets(4 & 5 & 6),
...goes on which will even create another type of subsets if possible which would be
type 5(1 & 2 & 3 & 4 & 5) in this case

Any method/ways are welcome as long as the results are the same which is the arraylist will contain the whole set of subsets base on the maximum condition.

Cheers,
Jaf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜血染红嫁衣 2024-09-25 00:41:26

听起来就像您可能想要power set(一组某个给定集合的所有子集),但从您的示例来看,您似乎想使用滑动窗口枚举列表中的所有子列表。

如果您想要幂集:

用于生成幂集的示例代码可在 此答案类似问题。

如果您想要滑动窗口子列表:

您可以使用下面的代码执行类似的操作。该示例准确地打印出上面的内容(在集合 {1, 2, 3, 4, 5} 上使用从 2 到 5 的滑动窗口大小)。

注意:为了清楚起见,下面的代码中没有边界检查或错误处理。绝对应该有一些。

class Program
{
    static void Main(string[] args)
    {
        foreach (var list in SlidingWindow<int>.Generate(new int[] { 1, 2, 3, 4, 5 }, 2, 5))
        {
            Console.WriteLine(string.Join(" & ", list));
        }
    }
}

static class SlidingWindow<T>
{
    static public IEnumerable<IEnumerable<T>> Generate(ICollection<T> items, int minWindowSize, int maxWindowSize)
    {
        for (int i = minWindowSize; i < maxWindowSize; ++i)
        {
            foreach (var list in Generate(items, i))
                yield return list;
        }
    }

    static public IEnumerable<IEnumerable<T>> Generate(ICollection<T> items, int windowSize)
    {
        for (int i = 0; i < (items.Count - windowSize + 1); ++i)
            yield return items.Skip(i).Take(windowSize);
    }
}

It sounds like you might want the power set (the set of all subsets of some given set), but from your example, it looks like you want to enumerate all sub-lists in a list using a sliding window.

If you want the power set:

Example code for generating power sets is available in this answer to a similar question.

If you want sliding window sub-lists:

You could do something like this with the code below. The example prints out exactly what you have above (using a sliding window size from 2 to 5 over the set {1, 2, 3, 4, 5}).

Note: For clarity's sake, there is no bounds-checking or error handling in the code below. There should absolutely be some.

class Program
{
    static void Main(string[] args)
    {
        foreach (var list in SlidingWindow<int>.Generate(new int[] { 1, 2, 3, 4, 5 }, 2, 5))
        {
            Console.WriteLine(string.Join(" & ", list));
        }
    }
}

static class SlidingWindow<T>
{
    static public IEnumerable<IEnumerable<T>> Generate(ICollection<T> items, int minWindowSize, int maxWindowSize)
    {
        for (int i = minWindowSize; i < maxWindowSize; ++i)
        {
            foreach (var list in Generate(items, i))
                yield return list;
        }
    }

    static public IEnumerable<IEnumerable<T>> Generate(ICollection<T> items, int windowSize)
    {
        for (int i = 0; i < (items.Count - windowSize + 1); ++i)
            yield return items.Skip(i).Take(windowSize);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文