Circular Permutation

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
349
Office Version
  1. 2003 or older
Platform
  1. 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:
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
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
The simple way would be
VBA Code:
Sub PTEST()

    Dim i
    Call PERMU("", "1234", 4)
    For i = 1 To WorksheetFunction.Fact(4 - 1)
        Debug.Print c.Item(i)
    Next i
    Set c = Nothing

End Sub
 
Upvote 0
@jasonb75 ... this particular workaround I was able to implement, wanted to check if there was a proper way rather than a workaround.
 
Upvote 0
The only other way would be to test each permutation before adding it to the collection, but given that the permutation has to be created before it can be tested I think it would be a false economy.
 
Upvote 0
@jasonb75 ... there is a second aspect too ... the order of the subsets is different in circular as against permutation. That is, with the (n-1) edit we get:
1​
2​
3​
4​
1​
2​
4​
3​
1​
3​
2​
4​
1​
3​
4​
2​
1​
4​
2​
3​
1​
4​
3​
2​

But, across examples of circular the order of the sets is:
1​
2​
3​
4​
1​
3​
2​
4​
1​
4​
2​
3​
1​
4​
3​
2​
1​
2​
4​
3​
1​
3​
4​
2​
 
Upvote 0
The edit matches your original example, which I believe is the default order of a collection. If you want something different then you will need a method that doesn't use collections.

You will also need to determine how the correct order is decided, there is no indication of that in the link that you provided, and the order of your second example above differs from the one in the link on all but the first and last rows.
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,412
Members
448,959
Latest member
camelliaCase

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top