Skip to content

Instantly share code, notes, and snippets.

@les-peters
Created December 19, 2022 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save les-peters/10d73c7f7c1477fc6e48f16a49a3f970 to your computer and use it in GitHub Desktop.
Save les-peters/10d73c7f7c1477fc6e48f16a49a3f970 to your computer and use it in GitHub Desktop.
Capital After Vowel
question = """
Given a string, make every consonant after a vowel
uppercase. Can you do this with and without regex?
Example:
> capitalAfterVowel("hello world")
> "heLlo WoRld"
> capitalAfterVowel("xaabeuekadii")
> "xaaBeueKaDii"
"""
import re
def capitalAfterVowelWithRE(str):
out = str
finds = re.findall(r'([aeiou])([^aeiou])', str)
for find in finds:
old = ''.join(find)
new = find[0] + find[1].upper()
out = re.sub(old, new, out, 1)
return out
def capitalAfterVowelWithoutRE(str):
vowels = [97, 101, 105, 111, 117]
out = ''
found_vowel = False
for ch in str:
if ord(ch) in vowels:
found_vowel = True
elif ord(ch) != 32 and found_vowel is True:
ch = chr(ord(ch) - 32)
found_vowel = False
out = out + ch
return out
print(capitalAfterVowelWithRE("hello world"))
print(capitalAfterVowelWithRE("xaabeuekadii"))
print(capitalAfterVowelWithoutRE("hello world"))
print(capitalAfterVowelWithoutRE("xaabeuekadii"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment