ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   permutation generator help. (https://www.excelbanter.com/excel-programming/316447-permutation-generator-help.html)

chandrans[_2_]

permutation generator help.
 

I have come across this permutation generator code. But doesn'
understand it
Can anybody explain how this works?
where x will be the string input and y will be initially ""
Sub GetPermutation(x As String, y As String)
' The source of this algorithm is unknown

Dim i As Integer, j As Integer
j = Len(y)
If j < 2 Then
Cells(CurrentRow, 1) = x & y
CurrentRow = CurrentRow + 1
Else
For i = 1 To j
Call GetPermutation(x + Mid(y, i, 1), Left(y, i - 1)
Right(y, j - i))
Next
End If
End Su

--
chandran
-----------------------------------------------------------------------
chandrans's Profile: http://www.excelforum.com/member.php...fo&userid=1436
View this thread: http://www.excelforum.com/showthread.php?threadid=27734


keepITcool

permutation generator help.
 

it's a recursive procedure. (which calls itself)
NOTE the description you give of the initial arguments s/b reversed!

to run it you'd call it like:

Dim currentrow As Long

Sub Foo()
currentrow = 1
Call GetPermutation("", "ABCDEF")
End Sub

and it will run the 720 (=PERMUT(6,5) permutations of the
letters "A thru F"



--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam

chandrans wrote in message
:


I have come across this permutation generator code. But doesn't
understand it
Can anybody explain how this works?
where x will be the string input and y will be initially ""
Sub GetPermutation(x As String, y As String)
' The source of this algorithm is unknown

Dim i As Integer, j As Integer
j = Len(y)
If j < 2 Then
Cells(CurrentRow, 1) = x & y
CurrentRow = CurrentRow + 1
Else
For i = 1 To j
Call GetPermutation(x + Mid(y, i, 1), Left(y, i - 1) +
Right(y, j - i))
Next
End If
End Sub




Tom Ogilvy

permutation generator help.
 
The code probably came from John Walkenbach's site:

http://j-walk.com/ss/excel/tips/tip46.htm

if not, that is a good place to start.

--
Regards,
Tom Ogilvy

"keepITcool" wrote in message
...

it's a recursive procedure. (which calls itself)
NOTE the description you give of the initial arguments s/b reversed!

to run it you'd call it like:

Dim currentrow As Long

Sub Foo()
currentrow = 1
Call GetPermutation("", "ABCDEF")
End Sub

and it will run the 720 (=PERMUT(6,5) permutations of the
letters "A thru F"



--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam

chandrans wrote in message
:


I have come across this permutation generator code. But doesn't
understand it
Can anybody explain how this works?
where x will be the string input and y will be initially ""
Sub GetPermutation(x As String, y As String)
' The source of this algorithm is unknown

Dim i As Integer, j As Integer
j = Len(y)
If j < 2 Then
Cells(CurrentRow, 1) = x & y
CurrentRow = CurrentRow + 1
Else
For i = 1 To j
Call GetPermutation(x + Mid(y, i, 1), Left(y, i - 1) +
Right(y, j - i))
Next
End If
End Sub







All times are GMT +1. The time now is 11:57 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com