1

Hey I was trying to write a program which will remove the consecutive duplicate characters from a string.

for example:
string->aabbccde
first iteration: bbccde
second iteration: ccde
third Iteration: de

and de is the answer.

following is the program I wrote.

a = "aabbcs"
def remove_dups(st,ind):
    print st, ind
    st = st.replace(st[ind], "")
    print st, "in dups"
    find_dups(st)

def find_dups(text):
    s=text
    print s, "in find"
    ln = len(s)
    print ln
    fg = 0
    ind = 0
    if ln==1:
        print s, 'len'
        return s
    for i in range(0,ln-1):
        if(s[i]==s[i+1]):
            ind = i
            remove_dups(s,ind)
    print s, 'check'        
    return s

ans = find_dups(a)
print 'answer', ans

and following is the output I am getting

aabbcs in find
6
aabbcs 0
bbcs in dups
bbcs in find
4
bbcs 0
cs in dups
cs in find
2
cs check
bbcs check
aabbcs 2
aacs in dups
aacs in find
4
aacs 0
cs in dups
cs in find
2
cs check
aacs check
aabbcs check
answer aabbcs

here above we have got cs but still answer is coming original string, I can understand it is because of recursion, but unable to understand how to resolve the issue. A little help would be appreciated. Thanks!

7 Answers 7

3

python have some easier way to do this, one of them:

>>> dup_string = 'aabcbccde'
>>> from itertools import groupby
>>> ''.join([x for x,y in groupby(dup_string) if sum(1 for i in y)<2])
'bcbde'
>>> dup_string = 'aabbccde'
>>> ''.join([x for x,y in groupby(dup_string) if sum(1 for i in y)<2])
'de'
>>> 
Sign up to request clarification or add additional context in comments.

2 Comments

I suspect that a string ending in ded will require an output of ded since there're not immediately consecutive letters. This will output de still... and abcbca will return an empty string and there's nothing consecutive at all...
this time should be ok.
2

If you're going to call the find_dups method recursively, you might as well get rid of the for loop. Just remove the consecutive duplicates as soon as you find them, and then recursively call find_dups again on the newly returned string.

a = "aabbcs"
def remove_dups(st,ind):
     return st.replace(st[ind:ind+1], "")


def find_dups(text, i):
    if len(text)-1 == i:
        return text
    if(text[i]==text[i+1]):
        text = remove_dups(text,i)
        text = find_dups(text, i)
    else:
        text = find_dups(text, i+1)
    return text

ans = find_dups(a, 0)
print "answer", ans

Comments

1

Your line remove_dups(s,ind) is the problem. It's not doing anything with the returned value. If you read through your code, in the top level function call you're assigning s=text at the top, then returning s at the bottom, without ever modifying the value of s. The clue is that you're printing the original text last, after you've printed the correct answer.
Try s = remove_dups(s, ind)

1 Comment

thanks I added s = remove_dups(s,ind) and this s = find_dups(s) rest it worked
1

You could easily do it using re.sub

import re
str = "aaaabbcccdddx"
print(re.sub(r"(.)\1+", '', str))

OP

x

1 Comment

Almost - you want '' though as the replacement string so the output matches the de the OP is expecting...
1

You should be returning the values of the string, since these are passed by copies. Also once you are done with remove_dups you should break out since you are no longer interested in the same you just modified.

a = "aabbcs"
def remove_dups(st,ind):
    print st, ind
    st = st.replace(st[ind], "")
    print st, "in dups"
    return find_dups(st)

def find_dups(text):
    s=text
    print s, "in find"
    ln = len(s)
    print ln
    fg = 0
    ind = 0
    if ln==1:
        print s, 'len'
        return s
    for i in range(0,ln-1):
        if(s[i]==s[i+1]):
            ind = i
            s = remove_dups(s,ind)
            break
    print s, 'check'        
    return s
ans = find_dups(a)
print 'answer', ans

Comments

0

bellow is your function to do the job

def remove_duplicates(str):
  integer=0
  while integer<len(str)-1:
      if str[integer]==str[integer+1]:
          str=str.replace(str[integer]," ",2)
      integer=integer+1    
  str=str.replace(" ","")
  print(str)

you can include the print statements that i left out!

Comments

0

For general list:

mylist = [['a'], ['a'], ['a'], ['b'], ['b'], ['c'], ['d'], ['e'], ['f'], ['a'], ['a']]
idx = 0
while True:
    if idx == len(mylist) - 1:
         break
    if mylist[idx] == mylist[idx + 1]:
         del mylist[idx + 1]
         idx = idx
    else:
         idx = idx + 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.