-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise35.py
More file actions
54 lines (35 loc) · 1.11 KB
/
Copy pathExercise35.py
File metadata and controls
54 lines (35 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def getTitleCase(text: str) -> str:
"""
Return a titlecase of the text
:param text: text in string
:type text: str
:raise TypeError: If text is not a str
:return: str
:rtype: str
"""
titleCase = ""
if len(text) == 0:
return titleCase
for i in range(len(text)):
if i == 0:
titleCase += text[i].upper()
elif text[i].isalpha() and not text[i - 1].isalpha():
titleCase += text[i].upper()
else:
titleCase += text[i].lower()
return titleCase
...
assert getTitleCase("Hello, world!") == "Hello, World!"
assert getTitleCase("HELLO") == "Hello"
assert getTitleCase("hello") == "Hello"
assert getTitleCase("hElLo") == "Hello"
assert getTitleCase("") == ""
assert getTitleCase("abc123xyz") == "Abc123Xyz"
assert getTitleCase("cat dog RAT") == "Cat Dog Rat"
assert getTitleCase("cat,dog,RAT") == "Cat,Dog,Rat"
import random
random.seed(42)
chars = list("abcdefghijklmnopqrstuvwxyz1234567890 ,.")
for i in range(1000):
random.shuffle(chars)
assert getTitleCase("".join(chars)) == "".join(chars).title()