Solved

Permutations of a list

Posted on 2004-03-22
1
606 Views
Last Modified: 2010-05-01
Another permutations question.

I have a list of words from which i need to create every 3, 4 ,5 etc permutation of i.e.

i have a list of 30 words and i want to create every possible 3 word permutation excluding permutations that contain duplicated words.

i want to have the ability to change the number of words in the permutations to whatever i need at the time.
0
Comment
Question by:Marcusw
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 10651932
Create a new project and add two textboxes, a commandbutton, and a listbox.  Enter the words separated by spaces in the first box.  Enter the number of words to use in the permuations in the second box.  Hit the button and the listbox will be populated.

Regards,

Idle_Mind

Option Explicit
Option Base 1

Private newWord() As String
Private used() As Boolean
Private letters() As String
Private wordLength As Long
Private words As Variant
Private wordCombos As Integer

Private Sub Form_Load()
    Text1.Text = "cat dog fish hamster"
    Text2.Text = "3"
    Command1.Caption = "Generate Permutations"
    wordCombos = 3
End Sub

Private Sub Command1_Click()
    Dim i As Long
    Dim word As String
    Dim fileName As String
   
    If Len(Text1.Text) > 0 And Len(Text2.Text) > 0 Then
        word = Trim(Text1.Text)
        words = Split(word)
        wordLength = UBound(words) + 1
       
        If IsNumeric(Text2.Text) Then
            wordCombos = CInt(Text2.Text)
            If wordCombos < 1 Or wordCombos > UBound(words) + 1 Then
                MsgBox "Invalid Number of Word Combinations"
                Exit Sub
            End If
        End If
       
        List1.Clear
       
        ReDim letters(wordLength)
        ReDim used(wordLength)
        ReDim newWord(wordLength)
        For i = 1 To wordLength
            letters(i) = i
            used(i) = False
        Next i
        Permute 1
    End If
End Sub

Private Sub Permute(n As Long)
    Dim i As Long
    Dim j As Long
    Dim outWord As String
   
    For i = 1 To wordLength
        If Not used(i) Then
            used(i) = True
            newWord(n) = letters(i)
            If n < wordCombos Then
                Permute (n + 1)
            Else
                outWord = words(CInt(newWord(1)) - 1)
                For j = 2 To wordCombos
                    outWord = outWord & " " & words(CInt(newWord(j)) - 1)
                Next j
                List1.AddItem outWord
            End If
            used(i) = False
        End If
    Next i
End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now