Solved

Factorial problem.

Posted on 2001-08-07
5
315 Views
Last Modified: 2008-03-04
I have this collection:

A,B,C,D,E

How can i find all possibilities between this collection? I know that 5 factorial=120 (this is the number of possibilities).

A,B,C,D,E
A,B,C,E,D
A,B,D,C,E
...

Can any one show me sme code to implement this.
I want to retrieve a collectionwith all possibilities.

Note: The initial collection can change from 5 to any number of elements.

Thanks

Carlos Rodrigues
0
Comment
Question by:Carlos_Rodrigues
5 Comments
 
LVL 5

Expert Comment

by:GeoffKell
ID: 6358918
0
 
LVL 18

Accepted Solution

by:
deighton earned 100 total points
ID: 6358930
Option Explicit

Private Sub Form_Load()

    Dim colX As New Collection
   
    Dim v, c
   
    Combinations "ABCDE", colX

    For Each v In colX
        c = c + 1
        Debug.Print c, v
    Next

End Sub

Private Function Combinations(x As String, col As Collection)

Dim sTest As String
Dim c As Long
Dim colX As Collection
Dim sString

If Len(x) > 1 Then

    For c = 1 To Len(x)

        sTest = Left(x, c - 1) & Mid(x, c + 1)
       
        Set colX = New Collection
       
        Combinations sTest, colX
   
        For Each sString In colX
           
            col.Add Mid(x, c, 1) & sString
           
        Next
       
        Set colX = Nothing
   
    Next

Else

    col.Add x

End If


End Function
0
 
LVL 3

Expert Comment

by:adg
ID: 6362068
Here is a non-recursive way:

Option Explicit

Dim colFac As New Collection
Dim DaRest As String

Private Sub Form_Load()
Dim s As Variant
Dim j As Integer, k As Integer

' seed the collection
colFac.Add "AB", "AB"
colFac.Add "BA", "BA"
DaRest = "CDE"

' generate combinations
For j = 1 To Len(DaRest)
    For Each s In colFac
        If Len(s) = j + 1 Then
            colFac.Remove s
            s = s + Mid(DaRest, j, 1)
            colFac.Add s, s
            For k = 1 To j + 1
                s = Right(s, j + 1) + Left(s, 1)
                colFac.Add s, s
            Next k
        End If
        DoEvents ' allows breaking out early
    Next s
Next j

' display results
For Each s In colFac
    List1.AddItem s
Next s

Caption = "Item Count = " + CStr(colFac.Count)
End Sub
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7198519
Hi Carlos_Rodrigues,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept deighton's comment(s) as an answer.
    *** GeoffKell's link is broken

Carlos_Rodrigues, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7213257
Per recommendation, force-accepted.

Netminder
CS Moderator
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Write data from SQL to CSV file - position 1 59
Vb.net 2008 2 59
JSON Response and request in VB6 application 11 392
Access 2013 combo box not working 3 43
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

805 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