Skip to content

Commit 3dba0bc

Browse files
committed
perf: optimize lower with list comprehension
1 parent 9b029f6 commit 3dba0bc

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

strings/lower.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ def lower(word: str) -> str:
1818
>>> lower("whAT")
1919
'what'
2020
"""
21-
result = []
22-
23-
for char in word:
24-
code = ord(char)
25-
if ASCII_UPPERCASE_START <= code <= ASCII_UPPERCASE_END:
26-
char = chr(code + ASCII_CASE_OFFSET)
27-
result.append(char)
28-
29-
return "".join(result)
21+
start = ASCII_UPPERCASE_START
22+
end = ASCII_UPPERCASE_END
23+
offset = ASCII_CASE_OFFSET
24+
25+
return "".join(
26+
[
27+
chr(code + offset) if start <= (code := ord(char)) <= end else char
28+
for char in word
29+
]
30+
)
3031

3132

3233
if __name__ == "__main__":

0 commit comments

Comments
 (0)