|
-
Nov 9th, 2004, 07:58 AM
#1
Thread Starter
Frenzied Member
Perm 3 from X
I am trying to get all different unique permutations of 2 numbers from X numbers
I then create folders based on the result.
I've made a fundamental error in the code below as it generates 234 and 243 which are (in this case) the same.
VB Code:
Dim intItems As Integer
Dim strCalcID As String
Dim i As Integer
Dim j As Integer
intItems = 6
For i = 1 To intItems - 2
For j = 2 To intItems - 1
If j = i Then
j = j + 1
End If
For k = 3 To intItems
If k = i Or k = j Then
k = k + 1
End If
'Write which items are used to variable
strCalcID = CStr(i & j & k)
'Check if directory exists
If Dir(strCalcID & "\") = "" Then
'Create subdirectory based upon item identifiers
fso.CreateFolder (strCalcID)
End If
Next
Next
Next
-
Nov 9th, 2004, 09:11 AM
#2
Lively Member
Hello agmorgan,
Just a couple of things I am not clear on.
1) You are not getting all the possible permutations i.e. you miss numbers like 111, any particular reason?
2)Why do you say that "234 and 243 which are (in this case) the same"? How are they the same?
As far as I have tested it your code seems to be technically OK (it runs without errors).
All the best,
Dave.
-
Nov 9th, 2004, 10:28 AM
#3
Thread Starter
Frenzied Member
It is used to select items from a list.
So as I need 3 separate items, I can skip things like 111 and 222 as they only include 1 item in each
234 and 243 are the same as they contain the same numbers
I think what I am trying to say is that the order of the numbers isnt significant.
-
Nov 9th, 2004, 02:40 PM
#4
why don't you explain what you're trying to do. there is prolly a better way to be doing it. if we know what you're doing, then we can offer suggestions. I don't understand.
-
Nov 9th, 2004, 04:20 PM
#5
Fanatic Member
Hi there, here is a modified simplified version of the routines I use for calculating permutations.
Start a new project, add a Command Button to it and then paste this code into the form module. You should be able to work out the rest from there (ask if you need more help).
VB Code:
Option Explicit
Private Row() As String
Private Sub Command1_Click()
CalculatePerms 5, 3
End Sub
Private Sub CalculatePerms(ByRef NoOfItems As Long, ByRef ItemsPerPerm As Long)
ReDim Row(ItemsPerPerm - 1)
Combinations 0, NoOfItems, 0, ItemsPerPerm - 1
End Sub
Private Sub Combinations(Start As Long, NoOfItems As Long, NodeIndex As Long, ItemsPerPerm As Long)
Dim Index As Long
If NodeIndex > ItemsPerPerm Then
'at this point here, the Row array holds the indexes of the items in this combination
MsgBox Join$(Row, ",")
Else
For Index = Start To NoOfItems
Row(NodeIndex) = "Item: " & CStr(Index)
'recursively generate combinations from i+1...n
Call Combinations(Index + 1, NoOfItems, NodeIndex + 1, ItemsPerPerm)
Next
End If
End Sub
Blade
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|