From f990d1bd32514d74148ce5113f22f7adfc9fd1ab Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 11 Apr 2026 18:35:10 +0530 Subject: [PATCH 1/4] Add input validation to reverse_letters function --- strings/reverse_letters.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/strings/reverse_letters.py b/strings/reverse_letters.py index cd1b7832d066..f3b5e7dadbb0 100644 --- a/strings/reverse_letters.py +++ b/strings/reverse_letters.py @@ -12,6 +12,12 @@ def reverse_letters(sentence: str, length: int = 0) -> str: >>> reverse_letters("racecar") 'racecar' """ + if not isinstance(sentence, str): + raise TypeError("sentence must be a string") + + if not isinstance(length, int) or length < 0: + raise ValueError("length must be a non-negative integer") + return " ".join( word[::-1] if len(word) > length else word for word in sentence.split() ) From 5d93c442a7fb56e83767dfad1b2abfc998ef7ed1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 Apr 2026 13:07:55 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/reverse_letters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/reverse_letters.py b/strings/reverse_letters.py index f3b5e7dadbb0..770796f8110e 100644 --- a/strings/reverse_letters.py +++ b/strings/reverse_letters.py @@ -17,7 +17,7 @@ def reverse_letters(sentence: str, length: int = 0) -> str: if not isinstance(length, int) or length < 0: raise ValueError("length must be a non-negative integer") - + return " ".join( word[::-1] if len(word) > length else word for word in sentence.split() ) From f6e7db6e33da05fd33e36776660ac11d6184056a Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 12 Apr 2026 14:48:10 +0530 Subject: [PATCH 3/4] Add input validation to palindrome functions --- strings/palindrome.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/strings/palindrome.py b/strings/palindrome.py index 4df5639b0c49..83fcdafc123a 100644 --- a/strings/palindrome.py +++ b/strings/palindrome.py @@ -24,8 +24,15 @@ def is_palindrome(s: str) -> bool: >>> all(is_palindrome(key) == value for key, value in test_data.items()) True + >>> is_palindrome(123) + Traceback (most recent call last): + ... + TypeError: Input must be a string """ + if not isinstance(s, str): + raise TypeError("Input must be a string") + start_i = 0 end_i = len(s) - 1 while start_i < end_i: @@ -44,6 +51,9 @@ def is_palindrome_traversal(s: str) -> bool: >>> all(is_palindrome_traversal(key) == value for key, value in test_data.items()) True """ + if not isinstance(s, str): + raise TypeError("Input must be a string") + end = len(s) // 2 n = len(s) @@ -63,6 +73,9 @@ def is_palindrome_recursive(s: str) -> bool: >>> all(is_palindrome_recursive(key) == value for key, value in test_data.items()) True """ + if not isinstance(s, str): + raise TypeError("Input must be a string") + if len(s) <= 1: return True if s[0] == s[len(s) - 1]: @@ -78,6 +91,9 @@ def is_palindrome_slice(s: str) -> bool: >>> all(is_palindrome_slice(key) == value for key, value in test_data.items()) True """ + if not isinstance(s, str): + raise TypeError("Input must be a string") + return s == s[::-1] From 49f57d298fb14fc673407f15ee4fb7c96ccb714e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 12 Apr 2026 09:20:55 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/palindrome.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/strings/palindrome.py b/strings/palindrome.py index 83fcdafc123a..7a513942c43d 100644 --- a/strings/palindrome.py +++ b/strings/palindrome.py @@ -53,7 +53,7 @@ def is_palindrome_traversal(s: str) -> bool: """ if not isinstance(s, str): raise TypeError("Input must be a string") - + end = len(s) // 2 n = len(s) @@ -75,7 +75,7 @@ def is_palindrome_recursive(s: str) -> bool: """ if not isinstance(s, str): raise TypeError("Input must be a string") - + if len(s) <= 1: return True if s[0] == s[len(s) - 1]: