|
| 1 | +""" |
| 2 | +Tests the performance of all of the solutions listed in the following article: |
| 3 | +https://therenegadecoder.com/code/how-to-capitalize-a-string-in-python/ |
| 4 | +""" |
| 5 | + |
1 | 6 | from test_bench import test_bench
|
2 | 7 |
|
3 |
| -def control(string): |
| 8 | + |
| 9 | +def control(_) -> None: |
| 10 | + """ |
| 11 | + Provides a control scenario for testing. In this case, none of the functions |
| 12 | + share any overhead, so this function is empty. |
| 13 | +
|
| 14 | + :param _: a placeholder for the string input |
| 15 | + :return: None |
| 16 | + """ |
4 | 17 | pass
|
5 | 18 |
|
6 |
| -def capitalize_by_hand(string): |
| 19 | + |
| 20 | +def capitalize_by_hand(string: str) -> str: |
| 21 | + """ |
| 22 | + Capitalizes a string by performing a code point shift |
| 23 | + for the first character. |
| 24 | +
|
| 25 | + :param string: an input string |
| 26 | + :return: the capitalized string |
| 27 | + """ |
7 | 28 | character = string[0]
|
8 | 29 | if 97 <= ord(character) <= 122:
|
9 |
| - shift = ord(character) - 32 |
| 30 | + shift = ord(character) - 32 |
10 | 31 | uppercase = chr(shift)
|
11 | 32 | return uppercase + string[1:]
|
12 | 33 | return string
|
13 |
| - |
14 |
| -def capitalize_by_mapping(string): |
| 34 | + |
| 35 | + |
| 36 | +def capitalize_by_mapping(string: str) -> str: |
| 37 | + """ |
| 38 | + Capitalizes a string by mapping between a set of lowercase |
| 39 | + characters and a set of uppercase characters. |
| 40 | +
|
| 41 | + :param string: an input string |
| 42 | + :return: the capitalized string |
| 43 | + """ |
15 | 44 | lowercase = "abcdefghijklmnopqrstuvwxyz"
|
16 | 45 | uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
17 | 46 | character = string[0]
|
18 | 47 | i = lowercase.find(character)
|
19 | 48 | return string if i == -1 else uppercase[i] + string[1:]
|
20 | 49 |
|
21 |
| -def capitalize_with_upper(string): |
| 50 | + |
| 51 | +def capitalize_with_upper(string: str) -> str: |
| 52 | + """ |
| 53 | + Capitalizes a string by leveraging the upper() method of |
| 54 | + strings on the first character. |
| 55 | +
|
| 56 | + :param string: an input string |
| 57 | + :return: the capitalized string |
| 58 | + """ |
22 | 59 | character = string[0]
|
23 | 60 | return character.upper() + string[1:]
|
24 | 61 |
|
25 |
| -def capitalize(string): |
| 62 | + |
| 63 | +def capitalize(string: str) -> str: |
| 64 | + """ |
| 65 | + Capitalizes a string by leveraging the capitalize() method. |
| 66 | + The behavior of this function differs from he previous |
| 67 | + functions because the capitalize() method also converts all |
| 68 | + other characters int he string to lowercase. |
| 69 | +
|
| 70 | + :param string: an input string |
| 71 | + :return: the capitalized string |
| 72 | + """ |
26 | 73 | return string.capitalize()
|
27 | 74 |
|
28 | 75 |
|
29 |
| -test_bench( |
30 |
| - [ |
31 |
| - control, |
32 |
| - capitalize_by_hand, |
33 |
| - capitalize_by_mapping, |
34 |
| - capitalize_with_upper, |
35 |
| - capitalize |
36 |
| - ], |
37 |
| - { |
38 |
| - "One Letter String": ["a"], |
39 |
| - "Small String": ["how now brown cow"], |
40 |
| - "Large String": ["One Punch Man" * 100] |
41 |
| - } |
42 |
| -) |
| 76 | +def main() -> None: |
| 77 | + """ |
| 78 | + Tests the performance of all the functions defined in this file. |
| 79 | + """ |
| 80 | + test_bench( |
| 81 | + [ |
| 82 | + control, |
| 83 | + capitalize_by_hand, |
| 84 | + capitalize_by_mapping, |
| 85 | + capitalize_with_upper, |
| 86 | + capitalize |
| 87 | + ], |
| 88 | + { |
| 89 | + "One Letter String": ["a"], |
| 90 | + "Small String": ["how now brown cow"], |
| 91 | + "Large String": ["One Punch Man" * 100] |
| 92 | + } |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + main() |
0 commit comments