From 74bc1630051ef0cd8625f330b9133131ed843f88 Mon Sep 17 00:00:00 2001 From: Milad Khoshdel Date: Sun, 6 Sep 2026 14:33:36 +0330 Subject: [PATCH] perf: optimize ASCII lowercase conversion --- strings/lower.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/strings/lower.py b/strings/lower.py index 49256b0169ef..d66dcd3b7a4e 100644 --- a/strings/lower.py +++ b/strings/lower.py @@ -1,6 +1,11 @@ +ASCII_UPPERCASE_START = ord("A") +ASCII_UPPERCASE_END = ord("Z") +ASCII_CASE_OFFSET = ord("a") - ord("A") + + def lower(word: str) -> str: """ - Will convert the entire string to lowercase letters + Convert ASCII uppercase letters in a string to lowercase. >>> lower("wow") 'wow' @@ -13,11 +18,15 @@ def lower(word: str) -> str: >>> lower("whAT") 'what' """ + result = [] + + for char in word: + code = ord(char) + if ASCII_UPPERCASE_START <= code <= ASCII_UPPERCASE_END: + char = chr(code + ASCII_CASE_OFFSET) + result.append(char) - # Converting to ASCII value, obtaining the integer representation - # and checking to see if the character is a capital letter. - # If it is a capital letter, it is shifted by 32, making it a lowercase letter. - return "".join(chr(ord(char) + 32) if "A" <= char <= "Z" else char for char in word) + return "".join(result) if __name__ == "__main__":