Question how to adapt string combination code for large list

cengineer

Member
Joined
Aug 17, 2009
Messages
8
Programming Experience
Beginner
I have the following code to generate combinations of string for a small list and would like to adapt this for a large list of over 300 string words.Can anyone suggest how to alter this code or to use a different method


VB.NET:
Public Class combinations 
 
 
 
Public Shared Sub main() 
 
    Dim myAnimals As String = "cat dog horse ape hen mouse" 
 
    Dim myAnimalCombinations As String() = BuildCombinations(myAnimals) 
 
    For Each combination As String In myAnimalCombinations 
        ''//Look on the Output Tab for the results!  
        Console.WriteLine("(" & combination & ")") 
    Next combination 
 
    Console.ReadLine() 
 
End Sub 
 
 
 
Public Shared Function BuildCombinations(ByVal inputString As String) As String() 
 
    ''//Separate the sentence into useable words.  
    Dim wordsArray As String() = inputString.Split(" ".ToCharArray) 
 
    ''//A plase to store the results as we build them  
    Dim returnArray() As String = New String() {""} 
 
    ''//The 'combination level' that we're up to  
    Dim wordDistance As Integer = 1 
 
    ''//Go through all the combination levels...  
    For wordDistance = 1 To wordsArray.GetUpperBound(0) 
 
        ''//Go through all the words at this combination level...  
        For wordIndex As Integer = 0 To wordsArray.GetUpperBound(0) - wordDistance 
 
            ''//Get the first word of this combination level  
            Dim combination As New System.Text.StringBuilder(wordsArray(wordIndex)) 
 
            ''//And all all the remaining words a this combination level  
            For combinationIndex As Integer = 1 To wordDistance 
 
                combination.Append(" " & wordsArray(wordIndex + combinationIndex)) 
 
            Next combinationIndex 
 
            ''//Add this combination to the results  
            returnArray(returnArray.GetUpperBound(0)) = combination.ToString 
 
            ''//Add a new row to the results, ready for the next combination  
            ReDim Preserve returnArray(returnArray.GetUpperBound(0) + 1) 
 
        Next wordIndex 
 
    Next wordDistance 
 
    ''//Get rid of the last, blank row.  
    ReDim Preserve returnArray(returnArray.GetUpperBound(0) - 1) 
 
    ''//Return combinations to the calling method.  
    Return returnArray 
 
End Function 
 
End Class
 
Any reason you can't get all the combinations starting with cat then move on to dog etc?

VB.NET:
Module Module1

    Sub Main()
        Dim myAnimals As String = "cat dog horse ape hen mouse"
        Dim myCombos As String() = GetCombinations(myAnimals.Split(" "c))
        Using sw As New IO.StreamWriter("C:\Temp\AnimalCombinations.txt")
            sw.Write(String.Join(Environment.NewLine, myCombos))
        End Using
    End Sub

    Public Function GetCombinations(ByVal inArray As String()) As String()
        Dim outList As New List(Of String)
        For elementIdx As Integer = 0 To inArray.Length - 1
            Dim combString As New System.Text.StringBuilder
            Dim wordIdx As Integer = elementIdx
            Do
                combString.Append(" " & inArray(wordIdx))
                outList.Add(combString.ToString.Trim)
                wordIdx += 1
            Loop While wordIdx < inArray.Length
        Next
        Return outList.ToArray()
    End Function

End Module

Output:

cat
cat dog
cat dog horse
cat dog horse ape
cat dog horse ape hen
cat dog horse ape hen mouse
dog
dog horse
dog horse ape
dog horse ape hen
dog horse ape hen mouse
horse
horse ape
horse ape hen
horse ape hen mouse
ape
ape hen
ape hen mouse
hen
hen mouse
mouse
 
Thanks for your response. I guess theres no real reason to keep the order except the example I was provided to follow was as such
n(n-1)/2 combinations

eg. 1,2,3,4,5,6,7,8,9,10
12,23,34,45,56,67,78,89,910
123,234,345,456,567,678,789,8910
1234,2345,3456,4567,5678,6789,78910
12345,23456,34567,45678,56789,678910
etc

order cannot change but selection can
 
Back
Top