Is there a way to remove contiguous vowels with an _ (underline)?

Question:

I’m solving this problem where I have to remove 3 contiguous vowel characters.

vowels = ['a','e','i','o','u'] 

For example:

input: 'happpyyyy Biiiirthdayyyyaaaa'
output:'happpyyyy B_irthdayyyy_a'

explanation: i & a are vowel chars and positioned contiguously. There are four occurrences of both i & a. but only three i & a will be removed.

code:

import re
def replaceV(strng):
    vowels = ['a','e','i','o','u']
    for i in strng:
        if strng.count(i) >= 3 and i in vowels:
            strng = strng.replace(i, '_')
    print(strng)
    remove_duplicates = re.sub(r'_+','_',strng)
    return remove_duplicates
            
print(replaceV('happpyyyy Biiiirthdayyyyaaaaa'))

The problem is it’s removing all vowel chars. but it should remove only 3 contiguous vowel chars.

Can I get help with this?

I tried using replace(i, ‘_’,3) but that is only removing first 3 occurrences, so that is no use.

Asked By: Abhijit Dey

||

Answers:

import re

def replaceV(strng):
    # Match a vowel and 2 more of the same vowel (3 total)
    return re.sub(r'([aeiou])1{2}', '_', strng)
            
print(replaceV('happpyyyy Biiiirthdayyyyaaaaa'))

Output:

happpyyyy B_irthdayyyy_aa
Answered By: Mark Tolonen

You can do this with regex.

r'([aeiou])1{2}'

  1. ([aeiou]) creates a group to capture vowels. It’s the first group so, it is referenced with 1
  2. 1{2} check if group 1 result repeats 2 more times
import re
from   functools import partial

#use partial to construct the known parts of sub
#basically we are just priming sub so the only data
#it needs is the input
vwl = partial(re.compile(r'([aeiou])1{2}').sub, '_')

in_ = 'happpyyyy Biiiirthdayyyyaaaa'

print(vwl(in_)) #happpyyyy B_irthdayyyy_a

note: compile is run before sub so the expression will only be compiled 1 time. This is more efficient than compiling the expression every time the sub function is called.

Answered By: OneMadGypsy
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.