Home > Backend Development > C++ > How to Generate All Possible Combinations of Array Elements Using LINQ's Cartesian Product?

How to Generate All Possible Combinations of Array Elements Using LINQ's Cartesian Product?

Barbara Streisand
Release: 2025-01-31 05:36:10
Original
363 people have browsed it

How to Generate All Possible Combinations of Array Elements Using LINQ's Cartesian Product?

Use linq to generate a variety of combinations

Give two array, ARRAY1 and Array2. We hope to create a possible combination of all elements in the first array by compressed the elements in the first array with the corresponding elements in the second array. For example, given:

Array1: {a, b, c}

    Array2: {3, 2, 4}
  • We want to generate the following combination:

This process requires us to obtain the fragment of the sequence of these two arrays. Linq provides a direct way to generate this Descartes:

<code>    a1 b1 c1
    a1 b1 c2
    a1 b1 c3
    a1 b1 c4

    a1 b2 c1
    a1 b2 c2
    a1 b2 c3
    a1 b2 c4


    a2 b1 c1
    a2 b1 c2
    a2 b1 c3
    a2 b1 c4

    a2 b2 c1
    a2 b2 c2
    a2 b2 c3
    a2 b2 c4


    a3 b1 c1
    a3 b1 c2
    a3 b1 c3
    a3 b1 c4

    a3 b2 c1
    a3 b2 c2
    a3 b2 c3
    a3 b2 c4 (最后一行)</code>
Copy after login
Create Descartes

This method obtains the sequence of the sequence, and use the Aggregate operator to accumulate the Descartes accumulation. It starts with an empty sequence as a cumulator and repeatedly combines it with each sequence in the input sequence. For each combination, it compresses the elements in the cumulator and the elements in the current sequence to generate the hydrochllar accumulation.

Compressed data
<code class="language-csharp">static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
{
    IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
    return sequences.Aggregate(
        emptyProduct,
        (accumulator, sequence) =>
            from accseq in accumulator
            from item in sequence
            select accseq.Concat(new[] { item })
    );
}</code>
Copy after login

Once we have the accumulation of Descartes, we can compress the elements in the first array and the corresponding element in the second array of the second array:

Cartesianproduct method allows us to easily generate the Descartes of multiple sequences. We create a sequence of a sequence by generating an integer range for each element in the second array, and then we compress these range of Descartes with the first array to create the required combination.

Finally, we can traverse the sequence of the sequence of the sequence and print each line:

<code class="language-csharp">var arr1 = new[] { "a", "b", "c" };
var arr2 = new[] { 3, 2, 4 };
var result = from cpLine in CartesianProduct(
                 from count in arr2 select Enumerable.Range(1, count))
             select cpLine.Zip(arr1, (x1, x2) => x2 + x1);</code>
Copy after login

This code will generate a combination as a string and print it to the console.

The above is the detailed content of How to Generate All Possible Combinations of Array Elements Using LINQ's Cartesian Product?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template