Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Permutations of a list

Posted on 2004-03-22
1
Medium Priority
?
620 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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 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…
Suggested Courses

609 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