Results 1 to 18 of 18

Thread: Recursive permutation

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    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.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Recursive permutation

    Quote Originally Posted by standardusr View Post
    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Re: Recursive permutation

    Quote Originally Posted by ChrisE View Post
    did you work out how many combinations there will be ?
    aaa,aab,aac,abb,abc,acc,bbb,bbc,bcc,ccc ....etc..?
    Yes.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    227

    Re: Recursive permutation

    Quote Originally Posted by LaVolpe View Post
    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. #6
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Recursive permutation

    Quote Originally Posted by standardusr View Post
    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.

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Recursive permutation

    Quote Originally Posted by ChrisE View Post
    OK
    that answer is helpfull to solve your problem
    Yes. ~smile~

  8. #8
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    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

  9. #9
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Recursive permutation

    Check this thread, with multiple samples:
    http://www.vbforums.com/showthread.p...te-permutation

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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

  12. #12
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Recursive permutation

    Quote Originally Posted by westconn1 View Post
    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

  13. #13
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Recursive permutation

    Quote Originally Posted by westconn1 View Post
    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.

  14. #14
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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

  15. #15
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    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. #16
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    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

  17. #17
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Recursive permutation

    Quote Originally Posted by SamOscarBrown View Post
    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.

  18. #18
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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
  •  



Click Here to Expand Forum to Full Width