adapting combination code for larger list

Posted by vbNewbie on Stack Overflow See other posts from Stack Overflow or by vbNewbie
Published on 2010-04-12T14:49:57Z Indexed on 2010/04/12 14:53 UTC
Read the original article Hit count: 302

Filed under:
|
|

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.

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

© Stack Overflow or respective owner

Related posts about visual

Related posts about visual-studio-2008