0

I am trying to remove consecutively same characters from a string. For example:

         abb --> ab
         aaab --> ab
         ababa --> ababa (since no two consecutive characters are same)

My code:

T=int(input())
l=[0]
S1=""
for i in range(T):
    S=input()
    for j in range(len(S)-1):
        if S[j]!=S[j+1]:
            if S[j] != l[len(l)-1]:
                l=[]
                l.append(S[j])
                l.append(S[j+1])
                print(l)

            for k in l:
                S1+=k
            

    print(S1)
    S1=""
    l=[0]

The code doesn't work for the third case (ababa). How do I fix this?

5 Answers5

2

One concise approach would use itertools.groupby:

from itertools import groupby

def clean(s):
    return ''.join(k for k, _ in groupby(s))

>>> clean("abb")
'ab'
>>> clean("aaab")
'ab'
>>> clean("ababa")
'ababa'

A rather simplified quadratic loop-based approach (linear in comments):

def clean(s):
    res = ""  # res = []
    for c in s:
        if not res or res[-1] != c:
            res += c  # res.append(c)
    return res  # return ''.join(res)
user2390182
  • 72,016
  • 6
  • 67
  • 89
1

A verbose way of doing it, may not be most efficient if the strings are large:

value = 'aaaaaabbbbaaaaaacdeeeeefff'

def no_dups(value):
    r = ''
    for i in value:
        if not r or r[-1] != i:
            r += i
    return r

print(no_dups(value))
# abacdef
Sazzy
  • 1,924
  • 3
  • 19
  • 27
1

Using regex, we could do re.sub(r'([a-z])\1+', r'\1', string_data)

import re

test_data = 'abb aaab ababa'.split()

for data in test_data:
    print(f"{data} -->", re.sub(r'([a-z])\1+', r'\1', data))
Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57
1

Came out with this code, works properly:

T=int(input())      #No of testcases; for testing multiple strings
S1=""
for i in range(T):
    S=input()
    for j in range(0,len(S),2):
        if j!=len(S)-1:
            if S[j]!=S[j+1]:
                S1+=S[j]
                S1+=S[j+1]
        else:
            if S1[len(S1)-1]!=S[j]:
                S1+=S[j]

    print(S1)
    S1=""
  
1

You can use regex as:

for char in set(string):
    string = re.sub(f'{char}+', char, string)
string

results in

 abb --> ab
 aaab --> ab
 ababa --> ababa
Hamza
  • 5,373
  • 3
  • 28
  • 43