Solved

Permutations of a list

Posted on 2004-03-22
1
612 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

738 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