forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlower.py
More file actions
36 lines (29 loc) · 712 Bytes
/
Copy pathlower.py
File metadata and controls
36 lines (29 loc) · 712 Bytes
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
ASCII_UPPERCASE_START = ord("A")
ASCII_UPPERCASE_END = ord("Z")
ASCII_CASE_OFFSET = ord("a") - ord("A")
def lower(word: str) -> str:
"""
Convert ASCII uppercase letters in a string to lowercase.
>>> lower("wow")
'wow'
>>> lower("HellZo")
'hellzo'
>>> lower("WHAT")
'what'
>>> lower("wh[]32")
'wh[]32'
>>> lower("whAT")
'what'
"""
start = ASCII_UPPERCASE_START
end = ASCII_UPPERCASE_END
offset = ASCII_CASE_OFFSET
return "".join(
[
chr(code + offset) if start <= (code := ord(char)) <= end else char
for char in word
]
)
if __name__ == "__main__":
from doctest import testmod
testmod()