Skip to content

Commit 99c22e5

Browse files
committed
feat(day 149): implement vowelcase transformed with vowels uppercased and consonants lowercased.
1 parent eeaac1b commit 99c22e5

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

FreeCodeCamp/CodingQ/vOwElcAsE.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
"""
2+
vOwElcAsE
3+
Given a string, return a new string where all vowels are converted to uppercase and all other alphabetical characters are converted to lowercase.
4+
5+
Vowels are "a", "e", "i", "o", and "u" in any case.
6+
Non-alphabetical characters should remain unchanged.
7+
"""
8+
9+
import unittest
10+
11+
class vowelcaseTest(unittest.TestCase):
12+
13+
def test1(self):
14+
self.assertEqual(vowel_case("vowelcase"), "vOwElcAsE")
15+
16+
def test2(self):
17+
self.assertEqual(vowel_case("coding is fun"), "cOdIng Is fUn")
18+
19+
def test3(self):
20+
self.assertEqual(vowel_case("HELLO, world!"), "hEllO, wOrld!")
21+
22+
def test4(self):
23+
self.assertEqual(vowel_case("git cherry-pick"), "gIt chErry-pIck")
24+
25+
def test5(self):
26+
self.assertEqual(vowel_case("HEAD~1"), "hEAd~1")
27+
28+
29+
def vowel_case(s):
30+
31+
vowels = "aeiouAEIOU"
32+
result = ""
33+
for char in s:
34+
if char in vowels:
35+
result += char.upper()
36+
else:
37+
if char.isalpha():
38+
result += char.lower()
39+
else:
40+
result += char
41+
42+
43+
return result
44+
45+
46+
"""
47+
48+
The above solution is correcct and nicely structures it does exactly what the problem asks.
49+
50+
51+
but we can make some refinments as well.
52+
53+
=> Efficiency: String concatenation (result += ...) in a loop is less efficient in Python because strings are immutable. A common
54+
pattern is to collect pieces in a list and ".join() at the end:
55+
=> The below soltuion is functionally identical, but faster for long strings.
56+
=> Simplification: You could avoid storing both uppercase and lowercase vowels by just checking char.lower()
57+
The above solution is correct but the only improvement is performance (using a list instead of repeated string concatenation) and possibly simplifying the vowel check.
58+
Otherwise, it's spot-on.
59+
"""
60+
61+
def vowel_case(s: str) -> str:
62+
vowels = "aeiou"
63+
result = []
64+
for ch in s:
65+
if ch.isalpha():
66+
if ch.lower() in vowels:
67+
result.append(ch.upper())
68+
else:
69+
result.append(ch.lower())
70+
else:
71+
result.append(ch)
72+
73+
return "".join(result)
74+
75+
"""
76+
=> ch.isalpha() in Python ensures we only transform letters.
77+
=> in Javascript, /[a-zA-Z]/.test(ch) checks if the character is alphabetic.
78+
=> Non-alphabetic characters (digits, punctuation, spaces) remain unchanges.
79+
=> The tranformation is consistent regardless of the original case.
80+
"""
81+
82+
def vowel_case_one_liner(s):
83+
vowels = "aeiou"
84+
return "".join(
85+
char.upper() if char.lower() in vowels else char.lower() if char.isalpha() else char
86+
for char in s
87+
)
88+
89+
import re
90+
91+
def vowel_case_regex(s: str) -> str:
92+
93+
def replacer(match):
94+
ch = match.group(0)
95+
return ch.upper() if ch.lower() in "aeiou" else ch.lower()
96+
return re.sub(r"[A-Za-z]", replacer,s)
97+
98+
99+
"""
100+
101+
=> re.sub(r"[A-Za-z]", replacer, s) finds all alphabetic characters.
102+
=> The replace function:
103+
-> Checks if the character is a vowel (aeiou).
104+
-> If yes -> uppercase
105+
-> If not -> lowercase
106+
=> Non-alphabetic characters are untouched because the regex only matches [A-Za-z].
107+
108+
This is concise , efficient, leverages regex's ability to target only the chararcters you care about.
109+
"""
110+
111+
if __name__ == "__main__":
112+
113+
print(vowel_case("Where there is a will there is a way"))
114+
unittest.main()

0 commit comments

Comments
 (0)