Solved

Permutations of a list

Posted on 2004-03-22
1
603 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBA Excel: fill / replace values based on a config sheet. 5 57
Use Multiple Forms 4 38
DIR issue 7 47
Vb.net 2008 2 51
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

760 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

23 Experts available now in Live!

Get 1:1 Help Now