Solved

Factorial problem.

Posted on 2001-08-07
313 Views
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
Question by:Carlos_Rodrigues

LVL 5

Expert Comment

ID: 6358918
0

LVL 18

Accepted Solution

deighton earned 100 total points
ID: 6358930
Option Explicit

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

End If

End Function
0

LVL 3

Expert Comment

ID: 6362068
Here is a non-recursive way:

Option Explicit

Dim colFac As New Collection
Dim DaRest As String

Dim s As Variant
Dim j As Integer, k As Integer

' seed the collection
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)
For k = 1 To j + 1
s = Right(s, j + 1) + Left(s, 1)
Next k
End If
DoEvents ' allows breaking out early
Next s
Next j

' display results
For Each s In colFac
Next s

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

LVL 49

Expert Comment

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.

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

ID: 7213257
Per recommendation, force-accepted.

Netminder
CS Moderator
0

Featured Post

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…