-
Sep 23rd, 2019, 01:27 AM
#1
Thread Starter
Addicted Member
Recursive permutation
Dear all expert programmers,
Please help for create recursive permutation like below.
Private Function getPermute(byval strText as string, byval bteLen as byte) as string
'Statement
End Function
Example
s=getPermute("ab",1)
result = a,b
s=getPermute("ab",2)
result = aa,ab,bb
s=getPermute("abc",3)
result = aaa,aab,aac,abb,abc,acc,bbb,bbc,bcc,ccc
Thank you very much.
Last edited by standardusr; Sep 25th, 2019 at 11:18 PM.
-
Sep 23rd, 2019, 05:59 AM
#2
Re: Recursive permutation
Have you searched the forum? This feels like a question that may have been asked many times before.
Tip: I have better luck using google: permuations vb6 site:vbforums.com
Most times, people here won't just create a potentially complex routine for you. We'd rather see you attempt the problem and post back with questions/problems you have.
You may need to explain better what that 2nd parameter is for. You named it bteLen. Your 3rd example passes 2 but the results contain 3 chars each? Confusing.
-
Sep 23rd, 2019, 07:47 AM
#3
Re: Recursive permutation
Originally Posted by standardusr
Dear all expert programmers,
Please help for create recursive permutation like below.
Private Function getPermute(byval strText as string, byval bteLen as byte) as string
'Statement
End Function
Example
s=getPermute("ab",1)
result = a,b
s=getPermute("ab",2)
result = aa,ab,bb
s=getPermute("abc",2)
result = aaa,aab,aac,abb,abc,acc,bbb,bbc,bcc,ccc
Thank you very much.
did you work out how many combinations there will be ?
aaa,aab,aac,abb,abc,acc,bbb,bbc,bcc,ccc ....etc..?
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Sep 25th, 2019, 11:18 PM
#4
Thread Starter
Addicted Member
Re: Recursive permutation
Originally Posted by ChrisE
did you work out how many combinations there will be ?
aaa,aab,aac,abb,abc,acc,bbb,bbc,bcc,ccc ....etc..?
Yes.
-
Sep 25th, 2019, 11:33 PM
#5
Thread Starter
Addicted Member
Re: Recursive permutation
Originally Posted by LaVolpe
Have you searched the forum? This feels like a question that may have been asked many times before.
Tip: I have better luck using google: permuations vb6 site:vbforums.com
Most times, people here won't just create a potentially complex routine for you. We'd rather see you attempt the problem and post back with questions/problems you have.
You may need to explain better what that 2nd parameter is for. You named it bteLen. Your 3rd example passes 2 but the results contain 3 chars each? Confusing.
Sorry sir, my 3rd example is below.
s=getPermute("abc",3)
result = aaa,aab,aac,abb,abc,acc,bbb,bbc,bcc,ccc
I try to search many times. But they are not just like I want.
-
Sep 26th, 2019, 12:19 AM
#6
Re: Recursive permutation
Originally Posted by standardusr
Yes.
OK
that answer is helpfull to solve your problem
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Sep 26th, 2019, 06:39 AM
#7
Re: Recursive permutation
Originally Posted by ChrisE
OK
that answer is helpfull to solve your problem
Yes. ~smile~
-
Sep 26th, 2019, 07:01 AM
#8
Re: Recursive permutation
and according to his sample-data i wouldn't be surprised if it's not a permutation he's looking for, but a combination.
Permutation: Order matters
Combination: Order doesn't matter
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Sep 26th, 2019, 08:03 AM
#9
Re: Recursive permutation
-
Sep 27th, 2019, 05:49 PM
#10
Re: Recursive permutation
it's not a permutation he's looking for, but a combination.
not sure on this, he only appears to want, for example, abb, not bab or bba as well
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 27th, 2019, 09:26 PM
#11
Re: Recursive permutation
You are correct, and it seems like an odd requirement to me, but apparently not as rare as one would think. Perhaps its modification of the problem to make it a little more challenging as a exercise.
However, Arnoutdv does give it away in post #7 of the thread he linked to.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Sep 28th, 2019, 02:07 AM
#12
Re: Recursive permutation
Originally Posted by westconn1
not sure on this, he only appears to want, for example, abb, not bab or bba as well
Permutation = Order of draw matters —> bab<>abb
Combination = Order doesn‘t matter —> bab=abb
in OP‘s case it‘s a Combination with repetition
e.g. classic Lottery or classic Poker is a combination without repetition.
a horse race is a permutation without repetition
a password-generator is a permutation with repetition
rolling three dice at the same time is combination with repetition
Last edited by Zvoni; Sep 28th, 2019 at 02:17 AM.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Sep 28th, 2019, 03:26 AM
#13
Re: Recursive permutation
Originally Posted by westconn1
not sure on this, he only appears to want, for example, abb, not bab or bba as well
Hi wes,
that's why I asked the OP in Post#3
what combinations he would like returned, it's a guessing game in the end
my guess is
Code:
Private Sub variation(ByVal Alphabet As String, ByVal Wort As String)
Dim i As Integer
Dim j As Integer
Dim tmpAlphabet As String
Dim tmpWort As String
Dim p As Integer
p = InStr(1, Wort, "*")
If p > 0 Then
If Mid(Wort, p, 1) = "*" Then
For j = 1 To Len(Alphabet)
tmpWort = Wort
tmpAlphabet = Alphabet
Mid(tmpWort, p, 1) = Mid(tmpAlphabet, j, 1)
variation tmpAlphabet, tmpWort
Next
End If
Else
Debug.Print Wort
'########## this would return :
'aaa
'aab
'aac
'aba
'abb
'abc
'aca
'acb
'acc
'baa
'bab
'bac
'bba
'bbb
'bbc
'bca
'bcb
'bcc
'caa
'cab
'cac
'cba
'cbb
'cbc
'cca
'ccb
'ccc
End If
End Sub
Private Sub Command1_Click()
Dim Wort As String
Wort = "abc"
variation Wort, String(Len(Wort), "*")
End Sub
Last edited by ChrisE; Sep 28th, 2019 at 03:30 AM.
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Sep 28th, 2019, 04:34 PM
#14
Re: Recursive permutation
ChrisE, you asked in post #3.
An example was given in post #5.
Your code doesn't match the example answer, i.e. your answer has trailing letters that are less then preceding letters.
As I mentioned (post #11), that post #7 in the thread that Arnoutdv linked to in post #9
http://www.vbforums.com/showthread.p...te-permutation
has a code example that meets the provided example in post #5. None of the trailing letters are less than preceding letters, i.e.
From post #5
s=getPermute("abc",3)
result = aaa,aab,aac,abb,abc,acc,bbb,bbc,bcc,ccc
Your results which include aba and aca are not valid.
Last edited by passel; Sep 28th, 2019 at 04:37 PM.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Sep 28th, 2019, 05:22 PM
#15
Re: Recursive permutation
am sure you are helping OP, passel...but you made me dizzy!: Post #3, post#5, post#11, post #7, post#9, post#5 (and once more, post#5)! :-)
-
Sep 28th, 2019, 07:10 PM
#16
Re: Recursive permutation
To give the current topic a bit more "math-context"...
It is not really a "permutation-problem", but falls into the class of "combinations", one where:
- "order does not matter"
- but also "repetition is allowed"
According to Wolfram-math: http://mathworld.wolfram.com/Multichoose.html
that kind of problem is often termed "Multichoose"- or "Bars & Stars"-problem...
As for "counting this" (with regards to the expected amount of resulting sets),
here is some code which does that in 3 functions:
Code:
Private Sub Form_Load()
Debug.Print "3 multichoose 2 = "; NmultichooseK(3, 2) '6
Debug.Print "3 multichoose 3 = "; NmultichooseK(3, 3) '10
Debug.Print "9 multichoose 5 = "; NmultichooseK(9, 5) '1287
End Sub
Function Factorial(ByVal n As Long) As Currency
Factorial = 1: For n = 1 To n: Factorial = Factorial * n: Next
End Function
Function NchooseK(ByVal n As Long, ByVal k As Long) As Currency
NchooseK = Factorial(n) / Factorial(k) / Factorial(n - k)
End Function
Function NmultichooseK(ByVal n As Long, ByVal k As Long) As Currency
NmultichooseK = NchooseK(n + k - 1, k)
End Function
And here something for the OP, which reproduces his Inputs recursively -
after adapting the routine Arnout (and Passel) have mentioned already:
Code:
Private Sub Form_Click()
Debug.Print Join(MultiChoose("ab", 1), ",")
Debug.Print Join(MultiChoose("ab", 2), ",")
Debug.Print Join(MultiChoose("abc", 2), ",")
Debug.Print Join(MultiChoose("abc", 3), ",")
End Sub
'returns an array of sets (where order doesn't matter and repetition is allowed)
'according to the amount of chars in the Input-Alphabet (n) and the desired SetLen (k)
'see also: http://mathworld.wolfram.com/Multichoose.html
Private Function MultiChoose(sInp$, ByVal SetLen&, Optional ByVal iStart&, Optional ByVal iDepth&)
Static SetMap$(): ReDim Preserve SetMap(0 To SetLen - 1)
If iDepth = SetLen Then MultiChoose = Join(SetMap, ""): Exit Function
For iStart = iStart To Len(sInp) - 1
SetMap(iDepth) = Mid(sInp, iStart + 1, 1)
MultiChoose = Trim(MultiChoose & " " & MultiChoose(sInp, SetLen, iStart, iDepth + 1))
Next
If iDepth = 0 Then MultiChoose = Split(MultiChoose, " ")
End Function
HTH
Olaf
-
Sep 29th, 2019, 12:40 AM
#17
Re: Recursive permutation
Originally Posted by SamOscarBrown
am sure you are helping OP, passel...but you made me dizzy!: Post #3, post#5, post#11, post #7, post#9, post#5 (and once more, post#5)! :-)
+1 :-))
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Sep 29th, 2019, 05:38 AM
#18
Re: Recursive permutation
Yes, I was a bit worried that the post #7 in the middle being from another thread might throw people off the rails...
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
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
|