0

I would like all possible combinations from two string arrays.

  • Both arrays must have same length.
  • Result must keep order

For example :

dim lStr1() as string = {"One", "Two", "Three"}
dim lStr2() as string = {"EditOne", "EditTwo", "EditThree"}

dim res() as string = myAwesomeFunction(lStr1, lStr2)

// res :
One Two Three
One Two EditThree
One EditTwo Three
One EditTwo EditThree
EditOne Two Three
EditOne Two EditThree
EditOne EditTwo Three
EditOne EditTwo EditThree

It's like the binary composition of 2 arrays of strings.

Testman
  • 57
  • 7

2 Answers2

1

The following code will produce the array in your example. It should work for any pair of input arrays. The function checks that the input arrays are of the same length.

The GetPermutations function is taken from a more general class I use for generating permutations of numbers. It returns arrays of total Integers between 0 and choose - 1, and being an Iterator function, it returns the next array each time it is called.

In order to match your example, I returned an array of String where each element is a single string consisting of each of the selected strings separated by spaces. You may find it more useful to return a List(Of String()) or even a List(Of List(Of String))

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim lStr1() As String = {"One", "Two", "Three"}
    Dim lStr2() As String = {"EditOne", "EditTwo", "EditThree"}

    Dim res() As String = myAwesomeFunction(lStr1, lStr2)
End Sub

Function MyAwesomeFunction(lStr1() As String, lStr2() As String) As String()
    Dim combos As New List(Of String)
    If lStr1.Length <> lStr2.Length Then Throw New ArgumentException("Arrays must have the same length")
    For Each combo() As Integer In GetPermutations(lStr1.Length, 2)
        Dim elem As New List(Of String)
        For i As Integer = 0 To combo.Length - 1
            elem.Add(If(combo(i) = 0, lStr1(i), lStr2(i)))
        Next
        combos.Add(String.Join(" ", elem))
    Next
    Return combos.ToArray
End Function

Public Iterator Function GetPermutations(choose As Integer, total As Integer) As IEnumerable(Of Integer())
    Dim totals() As Integer = Enumerable.Repeat(Of Integer)(total, choose).ToArray
    Dim value(choose - 1) As Integer

    Do
        Yield value
        For index As Integer = choose - 1 To 0 Step -1
            value(index) += 1
            If value(index) < totals(index) Then Continue Do
            value(index) = 0
        Next
        Exit Do
    Loop
End Function
Blackwood
  • 4,504
  • 16
  • 32
  • 41
1

Here's another solution. Since only 2 arrays are involved, we can bit-fiddle to get all of the "combinations". The & " " is just to format the output to match the example.

Private Function myAwesomeFunction(Array1() As String, Array2() As String) As String()
    If Array1.Length <> Array2.Length Then
        Throw New ArgumentException("Array lengths must be equal.")
    End If
    Dim combos(CInt(2 ^ Array1.Length) - 1) As String
    For i As Integer = 0 To combos.Count - 1
        For j = 0 To Array1.Length - 1
            If (i And (1 << j)) > 0 Then
                combos(i) += Array2(j) & " "
            Else
                combos(i) += Array1(j) & " "
            End If
        Next
    Next
    Return combos
End Function
SysDragon
  • 9,692
  • 15
  • 60
  • 89
JerryM
  • 910
  • 6
  • 9
  • isn't better to use If Array1.Length <> Array2.Length Then to compare size of array ? what is the difference between both ? – Testman Dec 16 '15 at 09:21
  • Length vs Count. According to [this](http://stackoverflow.com/questions/2521592/difference-between-ienumerable-count-and-length), the answer is, "yes!" Good idea! – JerryM Dec 16 '15 at 21:38