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.
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
You can do this with regex.
r'([aeiou])1{2}'
([aeiou])
creates a group to capture vowels. It’s the first group so, it is referenced with 1
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.
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.
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
You can do this with regex.
r'([aeiou])1{2}'
([aeiou])
creates a group to capture vowels. It’s the first group so, it is referenced with1
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.