Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 540a2c3

Browse files
Miladkhoshdelpre-commit-ci[bot]MaximSmolskiy
authored andcommitted
Simplify Capitalize Function (TheAlgorithms#12879)
* Simplify the capitalize function using ASCII arithmetic to make the algorithm five times faster. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update capitalize.py * Update capitalize.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
1 parent 6ba4fa8 commit 540a2c3

File tree

1 file changed

+1
-6
lines changed

1 file changed

+1
-6
lines changed

‎strings/capitalize.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from string import ascii_lowercase, ascii_uppercase
2-
3-
41
def capitalize(sentence: str) -> str:
52
"""
63
Capitalizes the first letter of a sentence or word.
@@ -19,11 +16,9 @@ def capitalize(sentence: str) -> str:
1916
if not sentence:
2017
return ""
2118

22-
# Create a dictionary that maps lowercase letters to uppercase letters
2319
# Capitalize the first character if it's a lowercase letter
2420
# Concatenate the capitalized character with the rest of the string
25-
lower_to_upper = dict(zip(ascii_lowercase, ascii_uppercase))
26-
return lower_to_upper.get(sentence[0], sentence[0]) + sentence[1:]
21+
return sentence[0].upper() + sentence[1:]
2722

2823

2924
if __name__ == "__main__":

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /