Skip to content

Commit a33e4f8

Browse files
authored
added more test case and type hint
1 parent e589293 commit a33e4f8

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

Diff for: strings/title.py

+34-19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
def convert_word(word: str) -> str:
2+
"""
3+
Converts a string to capitalized case, preserving the input as is
4+
5+
>>> convert_word("Aakash")
6+
'Aakash'
7+
8+
>>> convert_word("aakash")
9+
'Aakash'
10+
11+
>>> convert_word("AAKASH")
12+
'Aakash'
13+
14+
>>> convert_word("aAkAsH")
15+
'Aakash'
16+
"""
17+
18+
"""
19+
Convert the first character to uppercase if it's lowercase
20+
"""
21+
if 'a' <= word[0] <= 'z':
22+
word = chr(ord(word[0]) - 32) + word[1:]
23+
24+
"""
25+
Convert the remaining characters to lowercase if they are uppercase
26+
"""
27+
for i in range(1, len(word)):
28+
if 'A' <= word[i] <= 'Z':
29+
word = word[:i] + chr(ord(word[i]) + 32) + word[i+1:]
30+
31+
return word
32+
133
def to_title_case(input_str: str) -> str:
234
"""
335
Converts a string to title case, preserving the input as is
@@ -15,29 +47,12 @@ def to_title_case(input_str: str) -> str:
1547
'Aakash Giri'
1648
"""
1749

18-
def convert_word(word):
19-
"""
20-
Convert the first character to uppercase if it's lowercase
21-
"""
22-
if "a" <= word[0] <= "z":
23-
word = chr(ord(word[0]) - 32) + word[1:]
24-
25-
"""
26-
Convert the remaining characters to lowercase if they are uppercase
27-
"""
28-
for i in range(1, len(word)):
29-
if "A" <= word[i] <= "Z":
30-
word = word[:i] + chr(ord(word[i]) + 32) + word[i + 1 :]
31-
32-
return word
33-
3450
words = input_str.split()
3551
title_case_str = [convert_word(word) for word in words]
3652

37-
return " ".join(title_case_str)
38-
53+
return ' '.join(title_case_str)
3954

4055
if __name__ == "__main__":
4156
from doctest import testmod
4257

43-
testmod()
58+
testmod()

0 commit comments

Comments
 (0)