Home >Backend Development >C++ >How to Generate All Possible Combinations of Characters and Integers from Two Arrays Using LINQ?

How to Generate All Possible Combinations of Characters and Integers from Two Arrays Using LINQ?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-31 05:21:38774browse

How to Generate All Possible Combinations of Characters and Integers from Two Arrays Using LINQ?

generate all possible combinations

Problem description

Give two array, array1 contains characters, and array2 contains integer. How can we generate all possible combinations of these elements, forming "a (i) b (j) c (k) n (p)"? These combinations should traverse all possible I, J, K, etc. These values ​​are determined by the corresponding elements in Array2.

Answer

To generate all possible combinations with linq, we can use technology called "Descartes". This process involves creating a sequence, which contains each possible element combination from multiple input sequences.

Descartes accumulation

The following code defines an extension method

, it executes the slidel accumulation of any number sequence:

CartesianProduct Generate string combination

<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>
Once we have the

method, the string of the formatting format becomes very simple:

CartesianProduct In this example, the

variable contains a sequence of a string sequence, each of which represents a combination of a 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(i => i.ToString()))
             select cpLine.Zip(arr1, (x1, x2) => x2 + "(" + x1 + ")");</code>

Output combination result To output combinations specified in the description of the question, we can use a simple cycle:

By applying the Method to the range of Descartes and the original character array of the ranging, we can effectively generate all possible combinations of the given element.

This Revied Answer Improves The Code Clarity and Directly Addresses The Privem Statement by Generations in the "A (I) B (J)" Format. The
<code class="language-csharp">foreach (var line in result)
{
    Console.WriteLine(string.Join(" ", line));
}</code>
Method Remains Efficient for Handling Multiple sequences.

The above is the detailed content of How to Generate All Possible Combinations of Characters and Integers from Two Arrays Using LINQ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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