1. Recursive permutation

Dear all expert programmers,

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.

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.

3. Re: Recursive permutation

Originally Posted by standardusr
Dear all expert programmers,

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..?

4. 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.

5. 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.

6. Re: Recursive permutation

Originally Posted by standardusr
Yes.
OK

7. Re: Recursive permutation

Originally Posted by ChrisE
OK
Yes. ~smile~

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

9. Re: Recursive permutation

Check this thread, with multiple samples:

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

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.

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

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```

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
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.

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)! :-)

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)
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

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 :-))

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...

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured

Click Here to Expand Forum to Full Width