dcsimg
Results 1 to 3 of 3

Thread: [RESOLVED] Create all combination from SpinText - difficult algorithm help me

  1. #1

    Thread Starter
    Member Andres128's Avatar
    Join Date
    Jul 2014
    Posts
    49

    Resolved [RESOLVED] Create all combination from SpinText - difficult algorithm help me

    Hi all guys vbforums

    I have this string:

    Dim TEST As String = "{hello|hi|hi all} my name is {Jhon|Robert|David|Ana} and i am {1|2|3|4|5}"

    i NEED CREATE ALL COMBINATIONS POSSIBLE EXAMPLE:

    hello my name is Jhon and i am 1
    hi my name is Jhon and i am 2
    hi all my name is Jhon and i am 3
    hello my name is Jhon and i am 4
    hi my name is Jhon and i am 5

    hello my name is Robert and i am 1
    hi my name is Robert and i am 2
    hi all my name is Robert and i am 3
    hello my name is Robert and i am 4
    hi my name is Robert and i am 5

    hello all my name is David and i am 1
    hi all my name is David and i am 2
    hi all all my name is David and i am 3
    hello all my name is David and i am 4
    hi all my name is David and i am 5

    hello all my name is Ana and i am 1
    hi all my name is Ana and i am 2
    hi all all my name is Ana and i am 3
    hello all my name is Ana and i am 4
    hi all my name is Ana and i am 5

    ....................ALL COMBINATIONS

    Steeps:
    Get all {WORDS}


    Code:
    Dim SPINNER As String = "{hello|hi|hi all} my name is {Jhon|Robert|David|Ana} and i am {1|2|3|4|5}"
            Dim TEMP As String = SPINNER
            Dim rgg As String = "\{.*?\}"
    
            Dim RG = New Regex(rgg, RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant Or RegexOptions.Multiline Or RegexOptions.Singleline)
            Dim MTC As MatchCollection = RG.Matches(SPINNER)
    
            Dim count = 0
            Dim DIC_BEGIN, DIC_ALL As New Dictionary(Of String, String())
            For Each X As Match In MTC
                'ADD FIRST VALUE: {hello|hi|hi all}
                If DIC_BEGIN.Count = 0 Then
                    DIC_BEGIN.Add(count, X.Value.Replace("{", "").Replace("}", "").Split("|"))
                End If
    
                'ADD ALL VALUES: 
                DIC_ALL.Add(count, X.Value.Replace("{", "").Replace("}", "").Split("|"))
    
                TEMP = TEMP.Replace(X.Value, String.Format("[{0}]", count))
                count += 1
            Next
    
            'Now i need create all combinations of this:
            MsgBox(TEMP)
            count = 0
            Dim temp_ As New ArrayList
            For Each P_INICIAL In DIC_BEGIN
                For Each FIRST In P_INICIAL.Value
                    'hello
    
                    For Each x1 In DIC_ALL
                        If Not x1.Key = 0 Then
                            '2
                            For Each Second_ In x1.Value
                                temp_.Add(TEMP.Replace(String.Format("[{0}]", P_INICIAL.Key), FIRST) _
                                          .Replace(String.Format("[{0}]", x1.Key), Second_))
                            Next
                        End If
                    Next
                Next
                count += 1
            Next
    
            MsgBox(String.Join(vbNewLine, temp_.ToArray))


    My code not woring

    hello my name is Jhon and i am [2]
    hello my name is Robert and i am [2]
    hello my name is David and i am [2]
    hello my name is Ana and i am [2]
    hello my name is [1] and i am 1
    hello my name is [1] and i am 2
    hello my name is [1] and i am 3
    hello my name is [1] and i am 4
    hello my name is [1] and i am 5
    hi my name is Jhon and i am [2]
    hi my name is Robert and i am [2]
    hi my name is David and i am [2]
    hi my name is Ana and i am [2]
    hi my name is [1] and i am 1
    hi my name is [1] and i am 2
    hi my name is [1] and i am 3
    hi my name is [1] and i am 4
    hi my name is [1] and i am 5
    hi all my name is Jhon and i am [2]
    hi all my name is Robert and i am [2]
    hi all my name is David and i am [2]
    hi all my name is Ana and i am [2]
    hi all my name is [1] and i am 1
    hi all my name is [1] and i am 2
    hi all my name is [1] and i am 3
    hi all my name is [1] and i am 4
    hi all my name is [1] and i am 5

    Please help me brothers

    Thanks all for your time in help me

    Greetings....

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,962

    Re: Create all combination from SpinText - difficult algorithm help me

    Try this. I sorted by name and age, so all of the possible strings for Ana are listed first:

    Code:
    Dim TEST As String = "{hello|hi|hi all} my name is {Jhon|Robert|David|Ana} and i am {1|2|3|4|5}"
    Dim parts As New List(Of String)(TEST.Split("{"c, "}"c))
    parts.RemoveAll(Function(s) Not s.Contains("|"))
    
    For x As Integer = 0 To parts.Count - 1
        TEST = TEST.Replace(parts(x), x.ToString)
    Next
    
    Dim lengths As New List(Of Integer)
    For Each s As String In parts
        lengths.Add(s.Split("|"c).Length - 1)
    Next
    Dim m As Integer = lengths(0) + 1
    For x As Integer = 1 To lengths.Count - 1
        m *= (lengths(x) + 1)
    Next
    
    Dim indices() As Integer = Enumerable.Repeat(0, lengths.Count).ToArray
    Dim output As New List(Of String)
    For x As Integer = 0 To m - 1
        Dim fields() As String = Array.ConvertAll(Enumerable.Range(0, indices.Count).ToArray, Function(i) parts(i).Split("|"c)(indices(i)))
        output.Add(String.Format(TEST, fields))
        For y As Integer = indices.GetUpperBound(0) To 0 Step -1
            If indices(y) < lengths(y) Then
                indices(y) += 1
                Exit For
            Else
                indices(y) = 0
            End If
        Next
    Next
    output = output.OrderBy(Function(s) s.Split(New String() {"my name is "}, StringSplitOptions.None).Last).ToList
    MsgBox(String.Join(Environment.NewLine, output.ToArray))

  3. #3

    Thread Starter
    Member Andres128's Avatar
    Join Date
    Jul 2014
    Posts
    49

    Thumbs up Re: Create all combination from SpinText - difficult algorithm help me

    Quote Originally Posted by .paul. View Post
    Try this. I sorted by name and age, so all of the possible strings for Ana are listed first:

    Code:
    Dim TEST As String = "{hello|hi|hi all} my name is {Jhon|Robert|David|Ana} and i am {1|2|3|4|5}"
    Dim parts As New List(Of String)(TEST.Split("{"c, "}"c))
    parts.RemoveAll(Function(s) Not s.Contains("|"))
    
    For x As Integer = 0 To parts.Count - 1
        TEST = TEST.Replace(parts(x), x.ToString)
    Next
    
    Dim lengths As New List(Of Integer)
    For Each s As String In parts
        lengths.Add(s.Split("|"c).Length - 1)
    Next
    Dim m As Integer = lengths(0) + 1
    For x As Integer = 1 To lengths.Count - 1
        m *= (lengths(x) + 1)
    Next
    
    Dim indices() As Integer = Enumerable.Repeat(0, lengths.Count).ToArray
    Dim output As New List(Of String)
    For x As Integer = 0 To m - 1
        Dim fields() As String = Array.ConvertAll(Enumerable.Range(0, indices.Count).ToArray, Function(i) parts(i).Split("|"c)(indices(i)))
        output.Add(String.Format(TEST, fields))
        For y As Integer = indices.GetUpperBound(0) To 0 Step -1
            If indices(y) < lengths(y) Then
                indices(y) += 1
                Exit For
            Else
                indices(y) = 0
            End If
        Next
    Next
    output = output.OrderBy(Function(s) s.Split(New String() {"my name is "}, StringSplitOptions.None).Last).ToList
    MsgBox(String.Join(Environment.NewLine, output.ToArray))
    Hi .paul

    Your code is a eXtreme algorithm

    Working very good thanks you brother for your time and many greetings.

    Difficult to understand but I am a passionate programmer vb net, I study your code step by step

    Thread Solved

    Have a nice day .paul

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