在here(由@lazy dog)之后,我有一个很好的算法实现。然而,我需要这个在C#中,并且由于C#缺少yield from
以及可能是我自己的粗心,转换不是微不足道的。C#将一个列表分成n组的所有组合 - 从Python迁移代码
这是我目前有:
public static IEnumerable<ArrayList> sorted_k_partitions(int[] seq, int k) {
var n = seq.Length;
var groups = new ArrayList(); //a list of lists, currently empty
IEnumerable<ArrayList> generate_partitions(int i) {
if (i >= n) {
// this line was the bug, was not creating a
// deep clone of the list of lists
// yield return new ArrayList(groups);
yield return new ArrayList(groups.ToArray().Select(g => ((List<int>)g).ToList()));
// Ugly but that is because we are using ArrayList
// Using proper List<List<int>> cleans this up significantly
}
else {
if (n - i > k - groups.Count)
foreach (List<int> group in new ArrayList(groups)) {
group.Add(seq[i]);
// yield from generate_partitions(i + 1);
foreach (var d in generate_partitions(i + 1)) {
yield return d;
}
group.RemoveAt(group.Count - 1);
}
if (groups.Count < k) {
groups.Add(new List<int> {seq[i]});
foreach (var d in generate_partitions(i + 1)) {
// things start breaking down here, as this yield return
// appears to release flow control and we then get the
// yield return above. I have debuged this and the python
// version and the python version does not do this. Very hard
// to explain considering I don't fully understand it myself
yield return d;
}
groups.RemoveAt(groups.Count - 1);
}
}
}
return generate_partitions(0);
// don't worry about the sorting methods in the python
// version, not needed
}
任何人都可以看到任何明显的失误,我敢肯定,我缺乏的Python的yield from
和协同程序的理解在这里伤害我。
编辑:发现该错误,在上面添加评论
没有'yield return generate_partitions(i + 1)'试图产生一个'IEnumerable',我只需要产生一个'ArrayList'。我非常肯定,python的'generate_partitions'产出与'foreach(generate_particions中的var d)yield return d'非常相似。然而,在这种情况下,天真的类似的不错的enuf。 –
gatapia