-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathhow_to_capitalize_a_string.py
More file actions
83 lines (65 loc) · 2.17 KB
/
how_to_capitalize_a_string.py
File metadata and controls
83 lines (65 loc) · 2.17 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Tests the performance of all of the solutions listed in the following article:
https://therenegadecoder.com/code/how-to-capitalize-a-string-in-python/
"""
from test_bench import test_bench
def control(_) -> None:
"""
Provides a control scenario for testing. In this case, none of the functions
share any overhead, so this function is empty.
:param _: a placeholder for the string input
:return: None
"""
pass
def capitalize_by_hand(string: str) -> str:
"""
Capitalizes a string by performing a code point shift
for the first character.
:param string: an input string
:return: the capitalized string
"""
character = string[0]
if 97 <= ord(character) <= 122:
shift = ord(character) - 32
uppercase = chr(shift)
return uppercase + string[1:]
return string
def capitalize_by_mapping(string: str) -> str:
"""
Capitalizes a string by mapping between a set of lowercase
characters and a set of uppercase characters.
:param string: an input string
:return: the capitalized string
"""
lowercase = "abcdefghijklmnopqrstuvwxyz"
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
character = string[0]
i = lowercase.find(character)
return string if i == -1 else uppercase[i] + string[1:]
def capitalize_with_upper(string: str) -> str:
"""
Capitalizes a string by leveraging the upper() method of
strings on the first character.
:param string: an input string
:return: the capitalized string
"""
character = string[0]
return character.upper() + string[1:]
def capitalize(string: str) -> str:
"""
Capitalizes a string by leveraging the capitalize() method.
The behavior of this function differs from he previous
functions because the capitalize() method also converts all
other characters int he string to lowercase.
:param string: an input string
:return: the capitalized string
"""
return string.capitalize()
if __name__ == '__main__':
test_bench(
{
"One Letter String": ["a"],
"Small String": ["how now brown cow"],
"Large String": ["One Punch Man" * 100]
}
)