0

I'm trying to remove duplicates from my code but when this code encounters some cases, it fails with 'String Index Out Of Range' error.

eg., i/p - tadayutaysgcgtttggytytyyyikk o/p - tadayutaysgcgtgytytyik

def removeDups(str):
    smallOut = ''

    if len(str) == 1 or len(str) == 0:
        return str

    if str[0] == str[1]:
        smallOut = removeDups(str[2:])
        if str[1] == smallOut[0]:
            return smallOut
        else:
            return str[1] + smallOut
    else:
        smallOut = removeDups(str[1:])
        return str[0] + smallOut

string = input().strip()
print(removeDups(string))

i/p - tadayutaysgcgtttggytytyyyikk o/p - tadayutaysgcgtgytytyik

Georgy
  • 12,464
  • 7
  • 65
  • 73
Indraneil
  • 11
  • 2

2 Answers2

1

For removing duplicates you can use re module:

import re

s = 'tadayutaysgcgtttggytytyyyikk'

print( re.sub(r'(.)\1+', r'\1', s) )

Prints:

tadayutaysgcgtgytytyik

Or: without re, using itertools.groupby:

from itertools import groupby

s = 'tadayutaysgcgtttggytytyyyikk'

print(''.join(v for v, _ in groupby(s)))

Prints:

tadayutaysgcgtgytytyik
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0
I use a standard approach for removing consecutive duplicates. For each character, keep incrementing the pointer till the current and the next are same.
ans = ""

s = 'tadayutaysgcgtttggytytyyyikk'
i=0
while i<len(s):
    cnt = 1
    ans = ans+s[i]
    while i+1<len(s) and s[i]==s[i+1]:
        i+=1
    i+=1

ans
Parijat Bhatt
  • 664
  • 4
  • 6
  • Your post is currently [flagged as low-quality](https://stackoverflow.com/review/low-quality-posts/23749734). Adding an explanation as to how your code solves the problem could improve its quality, not just for the OP but for others readers as well. – Gino Mempin Aug 09 '19 at 00:05