Created
December 19, 2022 17:24
-
-
Save les-peters/10d73c7f7c1477fc6e48f16a49a3f970 to your computer and use it in GitHub Desktop.
Capital After Vowel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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