Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate all Combinations from Multiple (n) Lists

EDIT: I am completely redoing my questions as I have figured out the simplest way of asking it. Thanks to the commenters so far that got me thinking about the root problem.

public List<string> GetAllPossibleCombos(List<List<string>> strings)
{
    List<string> PossibleCombos = new List<string>();

    //????
    {
        string combo = string.Empty;
        // ????
        {
            combo += ????
        }
        PossibleCombos.Add(combo);
    }

    return PossibleCombos;
}

I need to figure out how to recursively go through each List<string> and combine 1 string from each list into a combo string. Don't worry too much about formatting the string as the "live" code uses a custom object instead. Also, feel free to assume that every list will contain at least 1 string and that there are no null values.

like image 991
Anthony Nichols Avatar asked Sep 14 '15 18:09

Anthony Nichols


People also ask

How do you generate all possible combinations of multiple lists?

Enter the formula =List1. Expand out the new List1 column and then Close & Load the query to a table. The table will have all the combinations of items from both lists and we saved on making a custom column in List1 and avoided using a merge query altogether!

How do you generate all possible combinations of two lists in Python?

The unique combination of two lists in Python can be formed by pairing each element of the first list with the elements of the second list. Method 1 : Using permutation() of itertools package and zip() function. Approach : Import itertools package and initialize list_1 and list_2.

How do you print all array combinations in Python?

Let the input array be {1, 2, 3, 4, 5} and r be 3. We first fix 1 at index 0 in data[], then recur for remaining indexes, then we fix 2 at index 0 and recur. Finally, we fix 3 and recur for remaining indexes. When number of elements in data[] becomes equal to r (size of a combination), we print data[].


1 Answers

Here is a simple non-recursive solution that just concatenates the elements of each combination:

public static List<string> GetAllPossibleCombos(List<List<string>> strings)
{
    IEnumerable<string> combos = new [] { "" };

    foreach (var inner in strings)
        combos = from c in combos
                 from i in inner
                 select c + i;

    return combos.ToList();
}

static void Main(string[] args)
{
    var x = GetAllPossibleCombos(
        new List<List<string>>{
            new List<string> { "a", "b", "c" },
            new List<string> { "x", "y" },
            new List<string> { "1", "2", "3", "4" }});
}

You could generalize this to return an IEnumerable<IEnumerable<string>>, which allows the caller to apply any operation they like for transforming each combination into a string (such as the string.Join below). The combinations are enumerated using deferred execution.

public static IEnumerable<IEnumerable<string>> GetAllPossibleCombos(
    IEnumerable<IEnumerable<string>> strings)
{
    IEnumerable<IEnumerable<string>> combos = new string[][] { new string[0] };

    foreach (var inner in strings)
        combos = from c in combos
                 from i in inner
                 select c.Append(i);

    return combos;
}

public static IEnumerable<TSource> Append<TSource>(
    this IEnumerable<TSource> source, TSource item)
{
    foreach (TSource element in source)
        yield return element;

    yield return item;
}

static void Main(string[] args)
{
    var combos = GetAllPossibleCombos(
        new List<List<string>>{
            new List<string> { "a", "b", "c" },
            new List<string> { "x", "y" },
            new List<string> { "1", "2", "3", "4" }});

    var result = combos.Select(c => string.Join(",", c)).ToList();
}
like image 74
Douglas Avatar answered Sep 25 '22 07:09

Douglas