Home > Enterprise >  Add a character before two consecutive vowels in a sentence? Python
Add a character before two consecutive vowels in a sentence? Python

Time:04-14

I'm trying to add the letter 'X' before each vowel in a string or sentence, however, when there is a repeated vowel, the letter 'X' should only be written once. For example, the word 'speeding' should look like this 'spXeedXing' but i'm getting 'spXeXedXing'.

I know why I'm getting this problem but don't know where to go from here to make it work.

Code below

def vowels(string):
    newString = ""
    for letter in string:
        if letter in "aeiou":
            newString  = "X"   letter
        else:
            newString  = letter

    print(newString)         
                
            
            
if __name__ == "__main__":
    vowels("speeding")

CodePudding user response:

>>> import re
>>> re.sub('([aeiou] )','X\g<1>','speeding')
'spXeedXing'
>>>

CodePudding user response:

You should create a new variable to track the previous letter in string. Check if the letters are continuous and only add X if the previous char is not the same as the current char.

def vowels(string):
    newString = ""
    i = 0 # Create a counter variable
    for letter in string:
        if (letter in "aeiou" and letter != string[i-1]) or (letter in "aeiou" and i == 0 and letter == string[i-1]): # Change this condition.  
            i  = 1 
            newString  = "X"   letter

        else:
            newString  = letter
            i  = 1 # Increment counter variable

    print(newString)

if __name__ == "__main__":
    vowels("speeding")

Output:

spXeedXing

Other test cases:

vowels("oompaloompas")
XoompXalXoompXas

vowels("eerie")
XeerXiXe

CodePudding user response:

You can let a regex do all the state-machine hard work...

To prepend 'X' to any number of consecutive vowels:

import re

s = 'speeding'
>>> re.sub(r'([aeiou] )', r'X\1', s)
'spXeedXing'

To prepend 'X' only to the same repeating vowel:

s = 'speeding'
>>> re.sub(r'(([aeiou])\2*)', r'X\1', s)
'spXeedXing'

s = 'toaster'
>>> re.sub(r'(([aeiou])\2*)', r'X\1', s)
'tXoXastXer'

CodePudding user response:

Here is a simple non-regex version:

def vowels(word):
    new_word = ""
    prev = ""  # no previous letter at first
    for letter in word:
        if letter in "aeiou" and letter != prev: 
            new_word  = "X"   letter
        else:
            new_word  = letter
        prev = letter  # keep to avoid insertion for repeats

    return new_word

if __name__ == "__main__":
    print(vowels("speeding"))
    print(vowels("eerie"))
    print(vowels("aaaaaaaaaaaaaargh"))

producing

spXeedXing
XeerXiXe
Xaaaaaaaaaaaaaargh

CodePudding user response:

you should try checking the previous letter in the string to see if it is the same letter as the current index

def vowels(string):
    newString = ""
    for i in range(len(string)):
        if string[i] in "aeiou":
            if string[i - 1] == string[i]:
                newString  = string[i]
            else:
                newString  = "X"   string[i]
        else:
            newString  = string[i]

print(newString)
  • Related