Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
862 views
in Technique[技术] by (71.8m points)

arrays - Permutations of string collections in C#

Seems like I'm stuck once again with recursive algorithms...

My application is supposed to sort files to different folders, according to the information specified by the user and according to a subfolder structure represented by a string like the following:

[ROOT]  brand  color  material  

The tags in the structure string represent collections:

Assume:

var brand = new List<string> { "Nike", "Adidas", "Reebok" };
var color = new List<string> { "red", "blue", "yellow", "black" };
var material = new List<string> { "leather", "fabric" };

var data = new List<List<string>>() { brand, color, material };

And what I 'm trying to get is something like:

[ROOT]Nike
edleather
[ROOT]Nike
edfabric
[ROOT]Nikelueleather
[ROOT]Nikeluefabric
[ROOT]Nikeyellowleather
[ROOT]Nikeyellowfabric
[ROOT]Nikelackleather
[ROOT]Nikelackfabric
[ROOT]Adidas
edleather
[ROOT]Adidas
edfabric
[ROOT]Adidaslueleather
[ROOT]Adidasluefabric
[ROOT]Adidasyellowleather
[ROOT]Adidasyellowfabric
[ROOT]Adidaslackleather
[ROOT]Adidaslackfabric
[ROOT]Reebok
edleather
[ROOT]Reebok
edfabric
[ROOT]Reeboklueleather
[ROOT]Reebokluefabric
[ROOT]Reebokyellowleather
[ROOT]Reebokyellowfabric
[ROOT]Reeboklackleather
[ROOT]Reeboklackfabric

The problem is that the amount of data tags (brand, color, material) and their order is not known in advance, hence the need for recursion.

Any idea?

Thank you so much in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is the code By Eric Lippert. Cartesian Product..

http://ericlippert.com/2010/06/28/computing-a-cartesian-product-with-linq/

public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
{
    // base case: 
    IEnumerable<IEnumerable<T>> result = new[] { Enumerable.Empty<T>() };
    foreach (var sequence in sequences)
    {
        var s = sequence; // don't close over the loop variable 
        // recursive case: use SelectMany to build the new product out of the old one 
        result =
            from seq in result
            from item in s
            select seq.Concat(new[] { item });
    }
    return result;
}

var result = CartesianProduct(new List<List<string>>() {brand,color,material });

Usage Example:

var brand = new List<string> { "Nike", "Adidas", "Reebok" };
var color = new List<string> { "red", "blue", "yellow", "black" };
var material = new List<string> { "leather", "fabric" };

foreach (var row in CartesianProduct(new List<List<string>>() { brand, color, material }))
{
    Console.WriteLine(String.Join(",", row));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...