需要递归地生成文件数组的每个唯一组合

Jas*_*uli 2 c# recursion combinations file factorial

我研究过很多类似的请求,但没有什么是我需要的.

这是我的问题.我在C#中工作,我有一个FileInfo []数组,其中包含未知数量的元素.

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
Run Code Online (Sandbox Code Playgroud)

我需要生成这些文件的每个可能的重新排序组合的列表,而不重复文件.

所以,我的一些结果将是这样的(例子不是代码格式):

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
Run Code Online (Sandbox Code Playgroud)

等等,直到我想出了所有可能的组合

因此,结果总数应该是数组中元素数量的阶乘.在这个例子中,有9个元素,因此应该有9*8*7*6*5*4*3*2*1 = 362,880种可能的组合.

我已经把这个搞乱了几天了,而我无法将它包裹起来.任何帮助表示赞赏,特别是代码示例!

谢谢!

Tho*_*que 5

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 };
Run Code Online (Sandbox Code Playgroud)

编辑:

这是一个通用解决方案,适用于任意数量的项目:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用方法如下:

IEnumerable<IEnumerable<FileInfo>> permutations = files.GetPermutations(9);
Run Code Online (Sandbox Code Playgroud)

(此解决方案的灵感来自Eric Lippert关于笛卡尔产品的文章.)


编辑2:

这是一个使用的变体Aggregate:

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 }));
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @Jason,这不是伪代码,我在LINQPad中运行它并且工作正常.它返回预期的排列数,第一个似乎是正确的(我当然没有检查所有362880个排列......) (2认同)