Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove certain consecutive duplicates in a string?

Tags:

python

I want to define a function that takes a string and a letter in that string and outputs a new string with the occurrences of the letter being once. For example

my_function("happy kitten","p")
'hapy kitten' 

or

my_function("happykitten","t") 
'happykiten'

I've tried

def my_function(string, lett):
newString = ""
for char in string: #all characters
    for s in string: #character I'm testing
        if s == len(s) > 1: 
            newString+=lett # if there are multiple occurrences of s, replace with lett since its a single char anyway
        else:
            newString+=char #if not a duplicate, add next char to newString
    return newString #("happy kitten","p") returns 'ppppppppppp'

and

def my_function(string, lett):
newString = ""
for char in string: #all characters
    for s in string: #character I'm testing
        if s == s+1: 
            newString+=lett # if there are multiple occurrences of s, replace with lett since its a single char anyway
        else:
            newString+=char #if not a duplicate, add next char to newString
    return newString #TypeError: cannot concatenate 'str' and 'int' objects

What't going wrong in my functions? Please no imports or built-in functions.

like image 228
user2041448 Avatar asked May 20 '26 03:05

user2041448


2 Answers

Well if you change your mind about the imports / built-in functions you can always do this:

from itertools import groupby

def my_function(s, c):
    return ''.join(c if a==c else ''.join(b) for a,b in groupby(s))

>>> from itertools import groupby
>>> def my_function(s, c):
...     return ''.join(c if a==c else ''.join(b) for a,b in groupby(s))
... 
>>> my_function("happy kitten","p")
'hapy kitten'
>>> my_function("happykitten","t")
'happykiten'
like image 120
arshajii Avatar answered May 22 '26 15:05

arshajii


Iterating over the characters is inefficient and most likely the wrong thing to do. Sounds really much like a homework assignment on a freshman course. In real life you should look into regular expressions and this question seems to provide an elegant answer.

Your problem is that you assume that s+1 points to the next value in the iterator and that is not a valid assumption. What you will have to do is record the sighting and on the next iteration act accordingly.

We can still solve this for the matter of practice:

def strip_duplicate_letters(input, letter):
  output = ''
  last = False

  for c in input:
    if c == letter:
      if last:
        continue
      else:
        last = True
    else:
      last = False
    output += c

  return output

This is such an elemental thing that you will have to think it thoroughly over to make sure you understand. Then forget the example and reproduce yourself.

Another way is to enumerate the letters to make index numbers available:

for i, c in enumerate(input):
  if i > 0 and c == letter and input[i-1] == letter:
    continue
  output += c

In case enumerate is too much of a problem, you can use an integer as counter and increment it.

i = 0
for c in input:
  ....
  i += 1
  ...
like image 23
ferrix Avatar answered May 22 '26 15:05

ferrix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!