1

Test Cases Input: abbbaaccada Output: ccada Input: bbccdddcb Output: (Empty string)

str = input("Enter string: ")

def my_string(string):
    if not string:
    return ""
    if len(string) == 1:
       return string
    if string[0] == string[1] == string[2]:
       return my_string(string[3:])
    return string[0] + my_string(string[1:])

print (my_string(str))

I am new to python. and I am trying to remove characters with 3 or more consecutive appearance in a string. In this I could only able to get output of only 1 iteration. e.g. i/p- hhhelllo o/p-eo but for i/p- abbbaaccada o/p is aaaccada but it should be ccada.. please help..

I have done this till 3 repetition but how to generalize it for more than 3 repetition.??

Bala Kiswe
  • 61
  • 1
  • 5
  • 2
    Possible duplicate of [Is there a fast algorithm to remove repeated substrings in a string?](https://stackoverflow.com/questions/43676557/is-there-a-fast-algorithm-to-remove-repeated-substrings-in-a-string) – Plasma Feb 20 '18 at 12:20
  • What would be the output for `bbbba`? `a` or `ba`? – Mr. T Feb 20 '18 at 12:25
  • It will be "ba"... That is another problem that I could not able to make it for more that 3 repetition... because I have put only 3 conditions in if loop.. How to generalize this for more than 3 appearance?? – Bala Kiswe Feb 20 '18 at 12:30

2 Answers2

3

Your problem presents the opportunity to show how else in for loops can be useful. Take a look:

def remover(my_str):
    temp = set(my_str)
    while True:
        for c in temp:
            if 3*c in my_str:
                my_str = my_str.replace(3*c, '')
                break
        else:
            break
    return my_str


test1 = 'abbbaaccada'
print(remover(test1))  # -> ccada

test2 = 'i/p- hhhelllo'
print(remover(test2))  # -> i/p- eo

If you insist on having recursive calls, you can modify the above as follows:

def remover(my_str):
    temp = set(my_str)
    new_str = my_str
    for c in temp:
        if 3*c in new_str:
            new_str = new_str.replace(3*c, '')
    if my_str == new_str:
        return new_str
    else:
        return remover(new_str)
Ma0
  • 15,057
  • 4
  • 35
  • 65
  • But I am trying to do it using recursive calls.. I will try to use recursive calls in your code.. If I could not do I will get back to you.. Thank you so much for the help – Bala Kiswe Feb 20 '18 at 12:53
  • @BalaKiswe Why the insistence on recursion, when a loop is perfectly fine? Is this an assignment? If so, please read [How do I ask homework questions?](https://meta.stackoverflow.com/a/334823/8881141) – Mr. T Feb 20 '18 at 13:02
  • @BalaKiswe I updated my answer with a recursive function as well. – Ma0 Feb 20 '18 at 13:03
1

I have added a solution which will work for 3 or more repetition as the above solution didn't work for me. It is a recursive solution.

import re

def format_string(u_str):
    f_str = remove_string(u_str)

    if f_str == u_str:
        return f_str
    else:
        return format_string(f_str)

def remove_string(u_str):
    index = 0  # This will maintain the index while traversing the entire string
    while index < len(u_str):
        r = re.search(u_str[index]*4 + '*', u_str)

        if r:
            start, end = r.span()  # start and end index of substring matching 3 or more repetition
            u_str = u_str[:start] + u_str[end:]  # removing the found substring
            index = end
        else:
            index += 1
    return u_str

test1 = 'abbbaaccada'
print('output:' + format_string(test1))

test2 = 'bbccdddcb'
print('output:' + format_string(test2))
Koshik Raj
  • 108
  • 6