Skip to content

Commit 388be53

Browse files
committed
feat(day 163): implement consonant case conversion with vowel/lowercase and hyphen handling
1 parent ecbf61f commit 388be53

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""
2+
3+
Consonant Case
4+
Given a string representing a variable name, convert it to consonant case using the following rules:
5+
6+
All consonants should be converted to uppercase.
7+
All vowels (a, e, i, o, u in any case) should be converted to lowercase.
8+
All hyphens (-) should be converted to underscores (_).
9+
"""
10+
11+
import unittest
12+
13+
class ConsonantCaseTest(unittest.TestCase):
14+
15+
16+
def test1(self):
17+
self.assertEqual(to_consonant_case("helloworld"), "HeLLoWoRLD")
18+
19+
def test2(self):
20+
self.assertEqual(to_consonant_case("HELLOWORLD"), "HeLLoWoRLD")
21+
22+
def test3(self):
23+
self.assertEqual(to_consonant_case("_hElLO-WOrlD-"), "_HeLLo_WoRLD_")
24+
25+
def test4(self):
26+
self.assertEqual(to_consonant_case("_~-generic_~-variable_~-name_~-here-~_"), "_~_GeNeRiC_~_VaRiaBLe_~_NaMe_~_HeRe_~_")
27+
28+
29+
def to_consonant_case(s):
30+
31+
vowels = 'aeiouAEIOU'
32+
res = ''
33+
34+
for char in s:
35+
if char.isalpha():
36+
if char in vowels:
37+
res += char.lower()
38+
else:
39+
res += char.upper()
40+
elif char == '-':
41+
res += '_'
42+
else:
43+
res += char
44+
45+
return res
46+
47+
48+
"""
49+
The above version is correct but there is a minor efficiency issue
50+
=> String concatenation in a loop res += char is fine for small inputs, but for very large strings, using a list and ""join() is more efficient:
51+
"""
52+
53+
def to_consonant_case(s):
54+
vowels = "aeiouAEIOU"
55+
56+
res = []
57+
58+
for char in s:
59+
if char.isalpha():
60+
res.append(char.lower() if char in vowels else char.upper())
61+
elif char == '-':
62+
res.append('_')
63+
else:
64+
res.append(char)
65+
66+
return "".join(res)
67+
68+
69+
70+
if __name__ == "__main__":
71+
unittest.main()

0 commit comments

Comments
 (0)