Skip to content
Advertisement

Remove the Last Vowel in Python

I have the following problem and I am wondering if there is a faster and cleaner implementation of the removeLastChar() function. Specifically, if one can already remove the last vowel without having to find the corresponding index first.

PROBLEM

Write a function that removes the last vowel in each word in a sentence.

Examples:

removeLastVowel("Those who dare to fail miserably can achieve greatly.")

“Thos wh dar t fal miserbly cn achiev gretly.”

removeLastVowel("Love is a serious mental disease.")

“Lov s serios mentl diseas”

removeLastVowel("Get busy living or get busy dying.")

“Gt bsy livng r gt bsy dyng”

Notes: Vowels are: a, e, i, o, u (both upper and lowercase).

MY SOLUTION

A PSEUDOCODE

  1. Decompose the sentence
  2. For each word find the index of the last vowel
  3. Then remove it and make the new “word”
  4. Concatenate all the words

CODE

JavaScript

OUTPUT

JavaScript

QUESTION

Can you suggest a better implementation of the removeLastChar() function? Specifically, if one can already remove the last vowel without having to find the corresponding index first.

Advertisement

Answer

This can be more easily achieved with a regex substitution that removes a vowel that’s followed by zero or more consonants up to a word boundary:

JavaScript

so that:

JavaScript

returns:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement