Combinations...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Hubbard

    Combinations...

    Does anyone have any code for doing combinations in VB.net or visual
    basic ? I'd like to
    be able to get an array of combinations (not permutations) of words from a
    base group of words while specifying the number of unique words to be used
    in the result set.

    For instance if I have the set {cat dog fish snake} I'd like to be able
    to get back a result array of all possible combinations (not permutations)
    of X number of words from my base set. For example, all possible
    combinations of any 3 words from the base set above would be :

    {cat dog fish}
    {cat dog snake}
    {cat fish snake}
    {dog fish snake}

    Ideally, I would like to be able to get a result array back with Y known
    words in it. For example, if I wanted all possible combinations of any 3
    words from the base set above again, but this time I only wanted resulting
    arrays that contained the words cat and snake the resulting array should
    look like:

    {cat dog snake}
    {cat fish snake}

    Anybody seen this code or at least the mathematical formulas needed to
    implement it?

    Thanks!







  • J French

    #2
    Re: Combinations...

    On Wed, 6 Aug 2003 23:04:45 -0400, "Jim Hubbard"
    <jim__hubbard@h otmail.com> wrote:
    [color=blue]
    > Does anyone have any code for doing combinations in VB.net or visual
    >basic ? I'd like to
    >be able to get an array of combinations (not permutations) of words from a
    >base group of words while specifying the number of unique words to be used
    >in the result set.
    >
    > For instance if I have the set {cat dog fish snake} I'd like to be able
    >to get back a result array of all possible combinations (not permutations)
    >of X number of words from my base set. For example, all possible
    >combinations of any 3 words from the base set above would be :
    >
    >{cat dog fish}
    >{cat dog snake}
    >{cat fish snake}
    >{dog fish snake}[/color]

    Something like this could do the trick :-

    Option Explicit: DefObj A-Z

    ' Combinations : J French 7th Aug 03

    Private Sub Command1_Click( )
    Dim Dest$

    Call LS_Combine(3, "", "ABCD", Dest$)
    Me.Print Dest$

    End Sub

    Private Sub LS_Combine(Samp leSize%, RootIn$, FullSetIn$, Dest$)
    Dim L9%, Root$

    ' --- At bottom - take result and exit
    If SampleSize = 0 Then
    Dest$ = Dest$ + RootIn$ + vbCrLf
    Exit Sub
    End If

    ' --- For each possible Start Letter
    For L9 = 1 To Len(FullSetIn$) - SampleSize + 1
    ' --- Build Root
    Root$ = RootIn$ + Mid$(FullSetIn$ , L9, 1)
    ' --- Handle Residue
    Call LS_Combine(Samp leSize% - 1, _
    Root$, _
    Mid$(FullSetIn$ , L9 + 1), _
    Dest$)
    Next

    End Sub

    Comment

    Working...