如何从迭代方法转向递归方法

时间:2014-08-20 09:47:58

标签: c# recursion combinatorics iteration

我有一个迭代的C#循环,它填充了最多5列的棋盘格式。

这些值是成对的,它始终是每个列的标题和多个值,并且它将这些值组合为非重复组合。

从我能想象到的最简单的解决方案开始,在看了之后,我认为必须通过递归方式更好地解决这个问题。

以下是我迄今为止尝试过的一个例子:

 List<EtOfferVariant> variants = new List<EtOfferVariant>();

 _containers[0].Variant.ForEach(first =>
 {
     if (_containers.Count > 1)
     {
         _containers[1].Variant.ForEach(second =>
         {
             if (_containers.Count > 2)
             {
                 _containers[2].Variant.ForEach(third =>
                     {
                         EtOfferVariant va = new EtOfferVariant();
                         va.OfferVariant1Type = _containers[0].VariantKey;
                         va.OfferVariant1 = first;
                         va.OfferVariant2Type = _containers[1].VariantKey;
                         va.OfferVariant2 = second;
                         va.OfferVariant3Type = third;
                         va.OfferVariant3 = _containers[3].VariantKey;
                         variants.Add(va);
                     });
             }
             else
             {
                 EtOfferVariant va = new EtOfferVariant();
                 va.OfferVariant1Type = _containers[0].VariantKey;
                 va.OfferVariant1 = first;
                 va.OfferVariant2Type = second;
                 va.OfferVariant2 = _containers[1].VariantKey;
                 variants.Add(va);
             }
         });
     }
     else
     {
         EtOfferVariant va = new EtOfferVariant();
         va.OfferVariant1Type = _containers[0].VariantKey;
         va.OfferVariant1 = first;
         variants.Add(va);
     }
 });

容器由字符串列表(值)和键(标题)组成。

在实际示例中,短片版本OfferVariant最多可达5个。

由于现有数据库给出的结果,我无法更改初始检查板结构。

下面是2个容器的数据输入和输出的说明,包括:

容器1:

  • Key:Pie
  • 值:
    1. Raspberry
    2. 草莓

容器2:

  • 钥匙:喝酒
  • 值:
    1. 可乐,
    2. 咖啡

生成的输出包含4行,包含

编辑因为它很容易被误解为如此处所示

结果将是包含4列

的数据库中的一行
Column 1 | Column 2  | Column 3 | Column 4
Pie      | Raspberry | Drink    | Cola 
Pie      | Raspberry | Drink    | Coffee
Pie      | Strawberry| Drink    | Cola 
Pie      | Strawberry| Drink    | Coffee

EtOfferVariant是包含这些列的ORM Poco

3 个答案:

答案 0 :(得分:1)

感谢您的回复, 马丁Liversage有点引导我的心理, 很难它不是一个纯粹的笛卡尔产品,因为事实被归为后者 和Cines方法给了我正确方向的最后一点我现在通过以下方式解决了这个问题

在第一步中我为第一个变体生成了初始行

    List<EtOfferVariant> row = new List<EtOfferVariant>();
    _containers.First().Variant.ForEach(o =>
    {
        row.Add(new EtOfferVariant() { OfferVariant1 = o, OfferVariant1Type = _containers.First().VariantKey });
    });
  return BuildVariants(row);

然后通过

运行它
  private List<EtOfferVariant> BuildVariants(List<EtOfferVariant> row, int containerIndex = 1)
    {
        List<EtOfferVariant> final = new List<EtOfferVariant>();
        row.ForEach(y =>
        {
            for (int i = 0; i < _containers[containerIndex].Variant.Count; i++)
            {
                var ret = MultiplyFromPrevious(y);
                FillByIndex(ret, _containers[containerIndex].Index, _containers[containerIndex].VariantKey, _containers[containerIndex].Variant[i]);
                final.Add(ret);
            }
        });
        containerIndex++;
        if (containerIndex < _containers.Count)
           return BuildVariants(final, containerIndex);
        return final;
    }

再次感谢Cine

private EtOfferVariant MultiplyFromPrevious(EtOfferVariant variant)
{
    EtOfferVariant ret = new EtOfferVariant();
    ret.OfferVariant1 = variant.OfferVariant1;
    ret.OfferVariant2 = variant.OfferVariant2;
    ret.OfferVariant3 = variant.OfferVariant3;
    ret.OfferVariant4 = variant.OfferVariant4;
    ret.OfferVariant5 = variant.OfferVariant5;

    ret.OfferVariant1Type = variant.OfferVariant1Type;
    ret.OfferVariant2Type = variant.OfferVariant2Type;
    ret.OfferVariant3Type = variant.OfferVariant3Type;
    ret.OfferVariant4Type = variant.OfferVariant4Type;
    ret.OfferVariant5Type = variant.OfferVariant5Type;

    return ret;
}

我还将列拆分为一个自己的方法,但它没有做任何事情,然后采取索引并将值映射到对象,没什么特别的

再次感谢大家,这真的放松了代码

答案 1 :(得分:0)

我认为你只想减少重复的代码。在这种情况下,只需添加您知道的数据,并在构建完成后添加变体。唯一的问题是,您需要一个复制构造函数来复制先前运行的值。

List<EtOfferVariant> variants = new List<EtOfferVariant>();

_containers[0].Variant.ForEach(first =>
{
    EtOfferVariant va = new EtOfferVariant();
    va.OfferVariant1Type = _containers[0].VariantKey;
    va.OfferVariant1 = first;
    if (_containers.Count > 1)
    {
        _containers[1].Variant.ForEach(second =>
        {
            va = new EtOfferVariant(va);
            va.OfferVariant2Type = _containers[1].VariantKey;
            va.OfferVariant2 = second;
            if (_containers.Count > 2)
            {
                _containers[2].Variant.ForEach(third =>
                {
                    va = new EtOfferVariant(va);
                    va.OfferVariant3Type = third;
                    va.OfferVariant3 = _containers[3].VariantKey;
                    variants.Add(va);
                });
            } else 
            variants.Add(va);
        });
    } else
    variants.Add(va);
});

...

public EtOfferVariant(EtOfferVariant va){
   this.OfferVariant1Type = va.OfferVariant1Type;
   this.OfferVariant2Type = va.OfferVariant2Type;
   this.OfferVariant3Type = va.OfferVariant3Type;
   this.OfferVariant1 = va.OfferVariant1;
   this.OfferVariant2 = va.OfferVariant2;
   this.OfferVariant3 = va.OfferVariant3;
}

答案 2 :(得分:0)

如果您首先转换容器中的词典,您似乎可以通过笛卡尔积获得想要的结果。

var containers = new List<Dictionary<string, IEnumerable<string>>>
                    {
                        new Dictionary<string, IEnumerable<string>>() {{"Pie", new [] {"Raspberry", "Strawbery"}}},
                        new Dictionary<string, IEnumerable<string>>() {{"Drink", new [] {"Cola", "Coffee"}}},
                        new Dictionary<string, IEnumerable<string>>() {{"Bread", new [] {"Bagel", "Pretzel", "Scone"}}},
                    };

var flatten = containers.Select(dict => dict.SelectMany(c => c.Value.Select(v => new {type = c.Key, name = v})));

foreach (var combo in CartesianProduct(flatten))
{
    Console.WriteLine(string.Join(", ", combo.Select(c => c.type + ": " + c.name)));
}

https://stackoverflow.com/a/3098381/44620

中的笛卡尔积法
public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(IEnumerable<IEnumerable<T>> sequences)
{
    IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
    return sequences.Aggregate(
        emptyProduct,
        (accumulator, sequence) => 
            from accseq in accumulator 
            from item in sequence 
            select accseq.Concat(new[] {item})                         
        );
}

输出:

Pie: Raspberry, Drink: Cola, Bread: Bagel
Pie: Raspberry, Drink: Cola, Bread: Pretzel
Pie: Raspberry, Drink: Cola, Bread: Scone
Pie: Raspberry, Drink: Coffee, Bread: Bagel
Pie: Raspberry, Drink: Coffee, Bread: Pretzel
Pie: Raspberry, Drink: Coffee, Bread: Scone
Pie: Strawbery, Drink: Cola, Bread: Bagel
Pie: Strawbery, Drink: Cola, Bread: Pretzel
Pie: Strawbery, Drink: Cola, Bread: Scone
Pie: Strawbery, Drink: Coffee, Bread: Bagel
Pie: Strawbery, Drink: Coffee, Bread: Pretzel
Pie: Strawbery, Drink: Coffee, Bread: Scone