From 3e43345627e3052e708d49d787d782182cc8fa8a Mon Sep 17 00:00:00 2001 From: Milad Khoshdel Date: Mon, 4 Aug 2025 20:28:22 +0330 Subject: [PATCH 1/4] Simplify the capitalize function using ASCII arithmetic to make the algorithm five times faster. --- strings/capitalize.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/strings/capitalize.py b/strings/capitalize.py index c0b45e0d9614..83065c600558 100644 --- a/strings/capitalize.py +++ b/strings/capitalize.py @@ -1,9 +1,6 @@ -from string import ascii_lowercase, ascii_uppercase - - def capitalize(sentence: str) -> str: """ - Capitalizes the first letter of a sentence or word. + Capitalizes the first character of the string if it is a lowercase letter. >>> capitalize("hello world") 'Hello world' @@ -19,11 +16,14 @@ def capitalize(sentence: str) -> str: if not sentence: return "" - # Create a dictionary that maps lowercase letters to uppercase letters - # Capitalize the first character if it's a lowercase letter - # Concatenate the capitalized character with the rest of the string - lower_to_upper = dict(zip(ascii_lowercase, ascii_uppercase)) - return lower_to_upper.get(sentence[0], sentence[0]) + sentence[1:] + # Get the first character of the sentence + first_char = sentence[0] + # Check if the first character is a lowercase letter + if 'a' <= first_char <= 'z': + # Convert the lowercase letter to uppercase using ASCII value + first_char = chr(ord(first_char) - 32) + # Return the capitalized first character concatenated with the rest of the sentence + return first_char + sentence[1:] if __name__ == "__main__": From 743f95aba5d07ba7d91d1c44791bd57ff8b47d69 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 Aug 2025 17:09:32 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/capitalize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/capitalize.py b/strings/capitalize.py index 83065c600558..ff7c5669e7ac 100644 --- a/strings/capitalize.py +++ b/strings/capitalize.py @@ -19,7 +19,7 @@ def capitalize(sentence: str) -> str: # Get the first character of the sentence first_char = sentence[0] # Check if the first character is a lowercase letter - if 'a' <= first_char <= 'z': + if "a" <= first_char <= "z": # Convert the lowercase letter to uppercase using ASCII value first_char = chr(ord(first_char) - 32) # Return the capitalized first character concatenated with the rest of the sentence From d219a3ce23755c6fd33e305b2f9befd9fffaa07e Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sun, 24 Aug 2025 13:00:17 +0300 Subject: [PATCH 3/4] Update capitalize.py --- strings/capitalize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/capitalize.py b/strings/capitalize.py index ff7c5669e7ac..a38e4a276bf3 100644 --- a/strings/capitalize.py +++ b/strings/capitalize.py @@ -1,6 +1,6 @@ def capitalize(sentence: str) -> str: """ - Capitalizes the first character of the string if it is a lowercase letter. + Capitalizes the first letter of a sentence or word. >>> capitalize("hello world") 'Hello world' From c22d8623d5f98a4f371d4da563e830b9afcb66de Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sun, 24 Aug 2025 13:02:23 +0300 Subject: [PATCH 4/4] Update capitalize.py --- strings/capitalize.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/strings/capitalize.py b/strings/capitalize.py index a38e4a276bf3..628ebffc8852 100644 --- a/strings/capitalize.py +++ b/strings/capitalize.py @@ -16,14 +16,9 @@ def capitalize(sentence: str) -> str: if not sentence: return "" - # Get the first character of the sentence - first_char = sentence[0] - # Check if the first character is a lowercase letter - if "a" <= first_char <= "z": - # Convert the lowercase letter to uppercase using ASCII value - first_char = chr(ord(first_char) - 32) - # Return the capitalized first character concatenated with the rest of the sentence - return first_char + sentence[1:] + # Capitalize the first character if it's a lowercase letter + # Concatenate the capitalized character with the rest of the string + return sentence[0].upper() + sentence[1:] if __name__ == "__main__":