Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 349
- Office Version
- 2003 or older
- Platform
- Windows
I have a code for Permutations which I am unable to modify for Circular Permutations.
Example of circular permutation:
CircularPermute
What is need is that, for example with a set 1234, instead of all 24 items, the code should generate only the first 6 (which is (n-1)! = (4-1)! = 6).
Desired output:
1234
1243
1324
1342
1423
1432
Code:
Example of circular permutation:
CircularPermute
What is need is that, for example with a set 1234, instead of all 24 items, the code should generate only the first 6 (which is (n-1)! = (4-1)! = 6).
Desired output:
1234
1243
1324
1342
1423
1432
Code:
VBA Code:
Option Explicit
Public c As New Collection
Sub PTEST()
Dim i
Call PERMU("", "1234", 4)
For i = 1 To c.count
Debug.Print c.item(i)
Next i
Set c = Nothing
End Sub
Sub PERMU(ByVal x As String, _
ByVal y As String, _
ByVal k As Long)
Dim s$
Dim i%
For i = 1 To Len(y)
s = x & Mid(y, i, 1)
If Len(s) = k Then
On Error Resume Next
c.Add s
Err.Clear
On Error GoTo 0
On Error GoTo -1
Else
End If
If Len(s) < k Then
PERMU CStr(s), Left(y, i - 1) & Mid(y, i + 1), k
End If
Next i
End Sub