Results 1 to 12 of 12

Thread: Algorithm to limit number of consecutive characters

  1. #1

    Thread Starter
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Algorithm to limit number of consecutive characters

    I need an algorithm that takes a string of characters and a number as parameters. It will output a string.

    The numeric parameter is the maximum number of identical characters that can be consecutive in the output string.

    The output string should contain the same characters, and be the same length, as the input string. The only difference is that the order of the characters in the output is such that the maximum consecutive characters is not exceeded.

    For example, F("aaaabbac", 2) = "aabaabac" would be a valid result.


    My thinking so far is along these lines:

    PHP Code:
    do loop
        start from left of string
        count consecutive characters
        
    if count max_consecutive then
            swap current character with next 
    (differentcharacter (may have to wrap round to the start of the string to find this)
        endif
    until no set of consecutive characters exceeds max_consecutive 
    I'd be grateful for comments and also some ideas on how to test the algorithm.
    This world is not my home. I'm just passing through.

  2. #2
    Addicted Member BestS's Avatar
    Join Date
    Mar 2005
    Posts
    222

    Re: Algorithm to limit number of consecutive characters

    Let's try something:
    Letter by letter we move through the string.
    aaaabaaaab
    First letter is "a" so we store it in a variable
    (dim strCheck as string, strAll as string)
    strAll = "aaaabaaaab"

    strCheck = left$("aaabaaab",1)
    Now we check the second, if it is again "a" then we check the third
    if it is again "a" then we replace it?
    Is this what you need?
    Using Visual Basic 6.0, access 2000, Visual Studio 2005

    Rate the post that you approve

  3. #3

    Thread Starter
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Algorithm to limit number of consecutive characters

    Yes, that sounds like what I'm talking about.

    The code I developed from my pseudo code (see earlier in this thread) is as follows:

    m_objMaterials is a collection that contains (for the sake of simplicity) single characters. In reality the collection contains objects that are identified by their TypeID property.

    MaxConsecutive is a function that returns the maximum number of consecutive characters.

    The idea is that you keep calling LimitConsecutive() until it returns False.


    The function seems to work ok, but there are three things I could do with help on:

    1. Could the function be more efficient and/or simpler.
    2. What is the maximum number of times the function has to be called to get the final result? (Obviously this is dependant on the number of different items, total quantity of items and the maximum consecutive items)
    3. How can I test the function to prove that it will work for all input.


    VB Code:
    1. Private Function LimitConsecutive() As Boolean
    2. 'Returns TRUE if materials have been swapped to prevent the maximum number of
    3. 'consecutive material types being exceeded.
    4.     Dim blnResult       As Boolean
    5.     Dim lngLoop         As Long
    6.     Dim lngIndex        As Long
    7.     Dim lngCount        As Long
    8.     Dim objTemp         As New CMaterials
    9.    
    10.     lngCount = 1
    11.    
    12.     For lngLoop = 1 To Size - 1
    13.         If m_objMaterials(lngLoop).TypeID = m_objMaterials(lngLoop + 1).TypeID Then
    14.             lngCount = lngCount + 1
    15.         Else
    16.             lngCount = 1
    17.         End If
    18.         If lngCount > MaxConsecutive Then
    19. 'There are too many of the same material type in a contiguous block.
    20.             blnResult = True
    21.             Set objTemp = m_objMaterials(lngLoop + 1)
    22.             lngIndex = lngLoop + 2
    23. 'Search forward to find a different material type.
    24.             If lngIndex <= Size Then
    25.                 While (lngIndex < Size) And (m_objMaterials(lngIndex).TypeID = objTemp.TypeID)
    26.                     lngIndex = lngIndex + 1
    27.                 Wend
    28.             Else
    29. 'Already at the end. Start again from the beginning.
    30.                 lngIndex = 1
    31.             End If
    32.             If m_objMaterials(lngIndex).TypeID <> objTemp.TypeID Then
    33. 'Found one - swap with the last one in the contiguous block.
    34.                 Set m_objMaterials(lngLoop + 1) = m_objMaterials(lngIndex)
    35.                 Set m_objMaterials(lngIndex) = objTemp
    36.                 Exit For
    37.             Else
    38. 'Continue to search from the beginning.
    39.                 lngIndex = 1
    40.                 While (lngIndex < lngLoop + 1) And (m_objMaterials(lngIndex).TypeID = objTemp.TypeID)
    41.                     lngIndex = lngIndex + 1
    42.                 Wend
    43.                 If m_objMaterials(lngIndex).TypeID <> objTemp.TypeID Then
    44. 'Found one now - swap with the last one in the contiguous block.
    45.                     Set m_objMaterials(lngLoop + 1) = m_objMaterials(lngIndex)
    46.                     Set m_objMaterials(lngIndex) = objTemp
    47.                     Exit For
    48.                 Else
    49.                     'Error - could not apply required limit.", , "TODO"
    50.                 End If
    51.             End If
    52.         End If
    53.     Next
    54.     LimitConsecutive = blnResult
    55. End Function
    This world is not my home. I'm just passing through.

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

    Re: Algorithm to limit number of consecutive characters

    VB Code:
    1. Dim maxl As Integer, mystr As String, s, mystart As Integer, pos As Integer, mychar As String
    2. Dim i As Integer
    3. maxl = 3
    4. mystart = 1
    5. pos = 1
    6. mystr = "qqqqqqqqqweeeerrrrrrrtttttttttyyuuuuuuuuuiiiiiooooooooppppppppp"
    7.  
    8. For i = 1 To Len(mystr)
    9.     mychar = String(maxl + 1, Mid(mystr, i, 1))
    10.     pos = InStr(i, mystr, mychar)
    11.     If Not pos = 0 Then
    12.     If pos = i Then
    13.         mystr = Left(mystr, i + maxl - 1) & Right(mystr, Len(mystr) - i - maxl) & Mid(mystr, i, 1)
    14.         i = i - 1
    15.         End If
    16.     End If
    17. Next
    18. Debug.Print mystr

    try this

    pete

  5. #5
    Addicted Member BestS's Avatar
    Join Date
    Mar 2005
    Posts
    222

    Re: Algorithm to limit number of consecutive characters

    Wow that's a long one...I can not even get it all right now...
    I gave just an idea that came to my mind:
    stplit the string letter by letter and then swap them, but you already SO...
    Using Visual Basic 6.0, access 2000, Visual Studio 2005

    Rate the post that you approve

  6. #6

    Thread Starter
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Algorithm to limit number of consecutive characters

    Thanks Pete,

    Even though I'm not actually dealing with strings in my real-world application it's a good idea. I can make a string based on the TypeIDs of my objects, apply your algorithm to it, and then reorder the collection based on the contents of the string.

    I tried your code with some different values as follows and it got stuck in a loop, but I can probably work that out for myself.

    Thanks again.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Dim maxl As Integer, mystr As String, s, mystart As Integer, pos As Integer, mychar As String
    5. Dim i As Integer
    6. maxl = 2
    7. mystart = 1
    8. pos = 1
    9. mystr = "11111112223"
    10.  
    11. For i = 1 To Len(mystr)
    12.     mychar = String(maxl + 1, Mid(mystr, i, 1))
    13.     pos = InStr(i, mystr, mychar)
    14.     If Not pos = 0 Then
    15.     If pos = i Then
    16.         mystr = Left(mystr, i + maxl - 1) & Right(mystr, Len(mystr) - i - maxl) & Mid(mystr, i, 1)
    17.         i = i - 1
    18.         End If
    19.     End If
    20. Next
    21. Debug.Print mystr
    22. End Sub
    This world is not my home. I'm just passing through.

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

    Re: Algorithm to limit number of consecutive characters

    it would have to get stuck if there are too many "1" s to split up

    if you get a string "pppppppppppppppppppppppppppppppppppp" no way you can make it do what you asked

    pete

  8. #8

    Thread Starter
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Algorithm to limit number of consecutive characters

    it would have to get stuck if there are too many "1" s to split up
    But in my example it should be able to transform 11111112223 to 11211211213.
    This world is not my home. I'm just passing through.

  9. #9
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: Algorithm to limit number of consecutive characters

    Try this one:

    VB Code:
    1. Private Function LimitConsecutive(ByRef mystr As String, ByVal MaxOccur As Integer) As Boolean
    2. Dim strResult As String
    3. Dim strBuffer As String
    4. Dim strChar As String
    5. Dim i As Integer
    6. Dim intOccur As Integer
    7.     ' loop through the string
    8.     For i = 1 To Len(mystr)
    9.         strChar = Mid(mystr, i, 1)
    10.         If i = 1 Then
    11.             ' the first character, just add it
    12.             strResult = strChar
    13.             intOccur = 1
    14.         Else
    15.             ' check if the character is the same as the last one
    16.             If strChar = Left(strResult, 1) Then
    17.                 ' do we exceed the maximum
    18.                 If intOccur >= MaxOccur Then
    19.                     ' add it to the buffer
    20.                     strBuffer = strBuffer & strChar
    21.                 Else
    22.                     ' add it to the result
    23.                     strResult = strResult & strChar
    24.                     intOccur = intOccur + 1
    25.                 End If
    26.             Else
    27.                 strResult = strResult & strChar
    28.                 ' if we have characters in the buffer, we can add them
    29.                 If Len(strBuffer) > 0 Then
    30.                     ' add characters from the buffer
    31.                     If Len(strBuffer) > MaxOccur Then
    32.                         ' buffer is larger the max occurence, add as much as is permitted
    33.                         strResult = strResult & Left(strBuffer, MaxOccur)
    34.                         intOccur = MaxOccur
    35.                         ' remove these characters from the buffer
    36.                         strBuffer = Right(strBuffer, Len(strBuffer) - MaxOccur)
    37.                     Else
    38.                         ' whole buffer can be added
    39.                         strResult = strResult & strBuffer
    40.                         intOccur = Len(strBuffer)
    41.                         ' empty the buffer
    42.                         strBuffer = ""
    43.                     End If
    44.                 Else
    45.                     intOccur = 1
    46.                 End If
    47.             End If
    48.          End If
    49.     Next
    50.     If Len(strBuffer) > 0 Then
    51.         ' there are still characters in the buffer, we didn't manage to perform the operation
    52.         LimitConsecutive = False
    53.         mystr = strResult & strBuffer
    54.     Else
    55.         ' buffer is empty, all is fine
    56.         LimitConsecutive = True
    57.         mystr = strResult
    58.     End If
    59. End Function

    You can use it like this:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim blnSucceeded As Boolean
    3. Dim MyString As String
    4.     MyString = "11111112223"
    5.     blnSucceeded = LimitConsecutive(MyString, 2)
    6.     If blnSucceeded Then
    7.         MsgBox MyString
    8.     Else
    9.         MsgBox "Failed"
    10.     End If
    11. End Sub
    Frans

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Algorithm to limit number of consecutive characters

    Quote Originally Posted by trisuglow
    But in my example it should be able to transform 11111112223 to 11211211213.
    If you already know the input data you don't need code. If you need code, your algorithm has to allow for input data of the form of "1111111111111111112". Your analysis can't handle that.

    Either the output data can't be the same length as the input data, the intervening items can't be taken solely from the input data or you have to rethink the whole thing.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  11. #11

    Thread Starter
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Algorithm to limit number of consecutive characters

    AI42,

    I can pre-validate the input to the alogrithm to ensure that a valid output is possible.
    This world is not my home. I'm just passing through.

  12. #12

    Thread Starter
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: Algorithm to limit number of consecutive characters

    FransC,

    Thanks, I'll have a look at this tomorrow.
    This world is not my home. I'm just passing through.

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