Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we remove word with repeated single character?

Tags:

python

regex

I am trying to remove word with single repeated characters using regex in python, for example :

good => good
gggggggg => g

What I have tried so far is following

re.sub(r'([a-z])\1+', r'\1', 'ffffffbbbbbbbqqq')

Problem with above solution is that it changes good to god and I just want to remove words with single repeated characters.

like image 944
Hrithik Puri Avatar asked Jan 27 '23 05:01

Hrithik Puri


2 Answers

A better approach here is to use a set

def modify(s):

    #Create a set from the string
    c = set(s)

    #If you have only one character in the set, convert set to string
    if len(c) == 1:
        return ''.join(c)
    #Else return original string
    else:
        return s

print(modify('good'))
print(modify('gggggggg'))

If you want to use regex, mark the start and end of the string in our regex by ^ and $ (inspired from @bobblebubble comment)

import re

def modify(s):

    #Create the sub string with a regex which only matches if a single character is repeated
    #Marking the start and end of string as well
    out = re.sub(r'^([a-z])\1+$', r'\1', s)
    return out

print(modify('good'))
print(modify('gggggggg'))

The output will be

good
g
like image 163
Devesh Kumar Singh Avatar answered Jan 29 '23 07:01

Devesh Kumar Singh


If you do not want to use a set in your method, this should do the trick:

def simplify(s):
  l = len(s)
  if l>1 and s.count(s[0]) == l:
    return s[0]
  return s

print(simplify('good'))
print(simplify('abba'))
print(simplify('ggggg'))
print(simplify('g'))
print(simplify(''))

output:

good
abba
g
g

Explanations:

  • You compute the length of the string
  • you count the number of characters that are equal to the first one and you compare the count with the initial string length
  • depending on the result you return the first character or the whole string
like image 27
Allan Avatar answered Jan 29 '23 08:01

Allan