C# Building Permutations with replacement 但如果某个位置不存在则跳过

C# Building Permutations with replacement but skip if it doesn't exist in a position

我想用替换进行排列,但如果符号不在列表中则跳过。 目前该实现不依赖于 PayDistribution。 它仅使用符号列表。 所以,如果我发送 (A,B,C) 的 symbolList 和 3 的 windowWidth,我将得到:

一个,一个,一个

A,A,B

A,A,C

A,B,A

A,B,B

...

C,C,C

但是如果我的 PayDistribution 是:

符号:第一到第三位

A:1 1 1

B:1 1 0

C:1 1 1

其中B不会在第三位,那么我不想包括任何B在第三位的排列:

例如A,A,B不应该存在。

我只是想让它跳到下一个有效条目。 似乎我可以在 GetCombinations 函数中添加一个检查来这样做,但我不确定如何。 真的想通过跳过来加快速度。对于较大的符号列表,可能需要一段时间。 我的用法不是很重要:

public ACombo[] PayHashLineCreate(SymbolList symbolList, ComboList comboTable, int windowWidth,List<List<int>> PayDistribution)
{
    Console.Write("Creating Pay Hash ");
    WindowWidth = windowWidth;
    SymbolCount = symbolList.NumRegularSym;

    var count = 0;

    var filteredList = new SymbolList();
    
    filteredList.AddRange(symbolList.Where(symbol => typeof (RegularSymbol) == symbol.GetType()));
    filteredList.AddRange(symbolList.Where(symbol => typeof (ScatterSymbol) == symbol.GetType()));
    var filteredSymbolListArray = filteredList.ToArray();

    var filteredComboList = new ComboList();
    filteredComboList.AddRange(comboTable.Where(combo => typeof (T) == combo.GetType()));

    foreach (
        var symbolArrangement in        
  CombinationsWithReplacement.GetCombinationsWithReplacementLexographicOrder(filteredSymbolListArray, windowWidth,PayDistribution))
    {
        Add(null);
        foreach (var combo in filteredComboList)
        {
            var match = Compare(combo, symbolArrangement);
            if (!match) continue;
            this[count] = combo;

            break;
        }
        count++;
    }
    Console.WriteLine("DONE");
    return ToArray(); 
}

但重要的功能:

public static IEnumerable<List<T>> GetCombinationsWithReplacementLexographicOrder<T>(IList<T> pool, 
int comboLength, List<List<int>> PayDistribution)
{
    
    foreach (var list in GetCombinations(pool, comboLength, PayDistribution).Select(c => 
c.ToList()))
    yield return list;
}

private static IEnumerable<IEnumerable<T>> GetCombinations<T>(IList<T> list, int length, 
List<List<int>> PayDistribution)
{
            
    if (length == 1) return list.Select(t => new[] { t });

    return GetCombinations(list, length -1).SelectMany(t => list, (t1, t2) => t1.Concat(new[] { t2 
}));
}

由于PayDistribution是position first,然后是symbol,所以可以直接转换为每个TDictionary,通过匹配Dictionary过滤组合每个职位:

private static IEnumerable<List<T>> GetCombinationsWithReplacementLexographicOrder<T>(IList<T> list, int length, List<List<int>> PayDistribution) {
    var PayMap = PayDistribution.Select(g => g.Select((pd,i) => (pd,i)).ToDictionary(pdi => list[pdi.i], pdi => pdi.pd > 0)).ToList();

    foreach (var c in GetCombinations(list, length, PayMap))
        yield return c.ToList();
}

private static IEnumerable<IEnumerable<T>> GetCombinations<T>(IList<T> list, int length, List<Dictionary<T, bool>> PayMaps, int pos = 1) {
    var payMap = PayMaps[length-pos];
    if (pos == length)
        return list.Where(t => payMap[t]).Select(t => new[] { t });

    return GetCombinations(list, length, PayMaps, pos + 1)
            .SelectMany(t => list.Where(t2 => payMap[t2]), (t1, t2) => t1.Concat(new[] { t2 }));
}

使用@NetMage 解决方案,我只需要稍微修改它以删除 Pivot() 调用并将 pdi.pd == 1 更改为 pdi.pd >0。在我的例子中,计数大于 1 是微不足道的。我只是不想用 0 创建一个排列。 现在,列表 A、B、C、D 或我想要的数量,将在 windowWidth(长度)位置数中创建带替换的排列,除非我向它发送一个特定位置为 0 的双列表。从而跳过无效排列。

public static IEnumerable<List<T>> GetCombinationsWithReplacementLexographicOrder<T>(IList<T> list, int length, List<List<int>> PayDistribution)
    {

        var PayMap = PayDistribution
            .Select(g => g.Select((pd, i) => (pd, i))
            .ToDictionary(pdi => list[pdi.i], pdi => pdi.pd >0)
            ).ToList();

        foreach (var c in GetCombinations(list, length, PayMap))
            yield return c.ToList();
    }

    private static IEnumerable<IEnumerable<T>> GetCombinations<T>(IList<T> list, int length, List<Dictionary<T, bool>> PayMaps, int pos = 1)
    {
        var payMap = PayMaps[length - pos];
        if (pos == length)
            return list.Where(t => payMap[t]).Select(t => new[] { t });

        return GetCombinations(list, length, PayMaps, pos + 1)
                .SelectMany(t => list.Where(t2 => payMap[t2]), (t1, t2) => t1.Concat(new[] { t2 }));
    }