Solved

Permutations of a list

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Specify ip address in my connection string for sas 2 58
passing parameter in sql procedure 9 61
Copy a row 12 61
Convert VB6 MSXML2.ServerXMLHTTP process to C# 2 46
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

816 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

9 Experts available now in Live!

Get 1:1 Help Now