需要递归地产生文件的阵列的每一个唯一组合 [英] Need to generate every unique combination of an array of files recursively
问题描述
我研究后发现的地段类似的请求,但没有什么是我需要什么比较
I've researched and found LOTS of similar requests, but nothing was quite what I needed.
下面是我的问题。我工作在C#和我有数目不详的IT元素的一个FileInfo []数组。
Here is my problem. I'm working in C#, and I have a FileInfo[] array with an unknown number of elements in it.
FileInfo[] files = new FileInfo[]
{
new FileInfo(@"C:\a.jpg"),
new FileInfo(@"C:\b.jpg"),
new FileInfo(@"C:\c.jpg"),
new FileInfo(@"C:\d.jpg"),
new FileInfo(@"C:\e.jpg"),
new FileInfo(@"C:\f.jpg"),
new FileInfo(@"C:\g.jpg"),
new FileInfo(@"C:\h.jpg"),
new FileInfo(@"C:\i.jpg"),
}; // Using 9 elements for this example
9个元素,我需要生成的每一个可能的组合,重新排序的列表。这些文件,而无需重复文件
And I need to generate a list of every possible reorder combination of these files, without repeating the files.
所以,我的一些结果会是这样(例如是不是在代码格式):
So, some of my results would be like this (example is not in code format):
a, b, c, d, e, f, g, h, i
a, b, c, d, e, f, g, i, h // i & h switched
a, b, c, d, e, f, h, g, i // last 3 elements switched
a, a, b, b, c, c, d, d, e // THIS IS NOT ACCEPTED, because elements are duplicated
和依此类推,直到我有拿出每一种可能组合
And so on, until I've come up with every possible combination
所以结果的总数应数组中元素的数目的阶乘。在本实施例中,有9个元素,所以应该有9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 362880种可能的组合。
So the total number of results should be the factorial of the number of elements in the array. In this example, there are 9 elements, so there should be 9*8*7*6*5*4*3*2*1=362,880 possible combinations.
我一直在搞乱这个几天,我只是不能完成我的脑海里解决它。任何帮助表示赞赏,特别是与代码示例!
I've been messing with this for a couple days now, and I just can't wrap my mind around it. Any help is appreciated, especially with code examples!
谢谢!
推荐答案
易与LINQ的:
IEnumerable<FileInfo[]> permutations =
from a in files
from b in files.Except(new[] { a })
from c in files.Except(new[] { a, b })
from d in files.Except(new[] { a, b, c })
from e in files.Except(new[] { a, b, c, d })
from f in files.Except(new[] { a, b, c, d, e })
from g in files.Except(new[] { a, b, c, d, e, f })
from h in files.Except(new[] { a, b, c, d, e, f, g })
from i in files.Except(new[] { a, b, c, d, e, f, g, h })
select new[] { a, b, c, d, e, f, g, h, i };
编辑:
下面是一个通用的解决方案,为任意数量的项目:
Here's a generic solution, for any number of items:
static class ExtensionMethods
{
public static IEnumerable<IEnumerable<T>> GetPermutations<T>(this IEnumerable<T> source, int count)
{
IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
for (int i = 0; i < count; i++)
{
result =
from seq in result
from item in source.Except(seq)
select seq.Concat(new[] { item });
}
return result;
}
}
如下使用它:
Use it as follows:
IEnumerable<IEnumerable<FileInfo>> permutations = files.GetPermutations(9);
(此解决方案是通过的埃里克利珀的有关笛卡尔乘积的。文章)
编辑2:
下面是一个使用变量汇总
:
static class ExtensionMethods
{
public static IEnumerable<IEnumerable<T>> GetPermutations2<T>(this IEnumerable<T> source, int count)
{
IEnumerable<IEnumerable<T>> seed = new[] { Enumerable.Empty<T>() };
return Enumerable.Repeat(source, count)
.Aggregate(
seed,
(accumulator, sequence) =>
from acc in accumulator
from item in sequence.Except(acc)
select acc.Concat(new[] { item }));
}
}
这篇关于需要递归地产生文件的阵列的每一个唯一组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!