From 02c4f086a164be6f2d582a014cdf740a9ccdf96f Mon Sep 17 00:00:00 2001 From: dhruvakidadumbe Date: Sat, 27 Dec 2025 22:18:57 +0530 Subject: [PATCH 1/8] Add docstring to palindrome function --- strings/palindrome.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/strings/palindrome.py b/strings/palindrome.py index e765207e5942..fa44a9797a25 100644 --- a/strings/palindrome.py +++ b/strings/palindrome.py @@ -19,6 +19,11 @@ def is_palindrome(s: str) -> bool: + """ + Check whether a given string is a palindrome. + + :param s: input string + :return: True if palindrome, False otherwise """ Return True if s is a palindrome otherwise return False. From 9cb62db7d913cb47ab441424fe8ee5d7f8aa9cc3 Mon Sep 17 00:00:00 2001 From: dhruvakidadumbe Date: Sat, 27 Dec 2025 22:21:30 +0530 Subject: [PATCH 2/8] Add docstring to palindrome function --- strings/palindrome.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/strings/palindrome.py b/strings/palindrome.py index fa44a9797a25..2308607765bf 100644 --- a/strings/palindrome.py +++ b/strings/palindrome.py @@ -25,12 +25,7 @@ def is_palindrome(s: str) -> bool: :param s: input string :return: True if palindrome, False otherwise """ - Return True if s is a palindrome otherwise return False. - - >>> all(is_palindrome(key) is value for key, value in test_data.items()) - True - """ - + start_i = 0 end_i = len(s) - 1 while start_i < end_i: From cf82a853983639daabe4a7bf8a86d5be03b190fc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 27 Dec 2025 16:58:52 +0000 Subject: [PATCH 3/8] [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 2308607765bf..f4e88c516733 100644 --- a/strings/palindrome.py +++ b/strings/palindrome.py @@ -21,11 +21,11 @@ def is_palindrome(s: str) -> bool: """ Check whether a given string is a palindrome. - + :param s: input string :return: True if palindrome, False otherwise """ - + start_i = 0 end_i = len(s) - 1 while start_i < end_i: From 2a695ffb49ab82f5571c8249e7726a7067d2950e Mon Sep 17 00:00:00 2001 From: dhruvakidadumbe Date: Sat, 27 Dec 2025 22:37:55 +0530 Subject: [PATCH 4/8] Add input validation and docstring to factorial function --- maths/factorial.py | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/maths/factorial.py b/maths/factorial.py index ba61447c7564..99c8988949c9 100644 --- a/maths/factorial.py +++ b/maths/factorial.py @@ -5,25 +5,13 @@ def factorial(number: int) -> int: """ - Calculate the factorial of specified number (n!). + + Calculate the factorial of a non-negative integer. - >>> import math - >>> all(factorial(i) == math.factorial(i) for i in range(20)) - True - >>> factorial(0.1) - Traceback (most recent call last): - ... - ValueError: factorial() only accepts integral values - >>> factorial(-1) - Traceback (most recent call last): - ... - ValueError: factorial() not defined for negative values - >>> factorial(1) - 1 - >>> factorial(6) - 720 - >>> factorial(0) - 1 + :param n: non-negative integer + :return: factorial of n + :raises ValueError: if n is negative + """ if number != int(number): raise ValueError("factorial() only accepts integral values") From 2f1cb5fcbc4857158663c3e10a5e2f87d0324cf3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 27 Dec 2025 17:42:04 +0000 Subject: [PATCH 5/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/factorial.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/factorial.py b/maths/factorial.py index 99c8988949c9..9082ad241dc3 100644 --- a/maths/factorial.py +++ b/maths/factorial.py @@ -5,13 +5,13 @@ def factorial(number: int) -> int: """ - + Calculate the factorial of a non-negative integer. :param n: non-negative integer :return: factorial of n :raises ValueError: if n is negative - + """ if number != int(number): raise ValueError("factorial() only accepts integral values") From 647b8997da50faa76814d7f82d01df2c157ff4b9 Mon Sep 17 00:00:00 2001 From: dhruvakidadumbe Date: Mon, 29 Dec 2025 14:10:42 +0530 Subject: [PATCH 6/8] Add input validation and docstring to prime check function --- maths/prime_check.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/maths/prime_check.py b/maths/prime_check.py index a757c4108f24..445cf8cd8da1 100644 --- a/maths/prime_check.py +++ b/maths/prime_check.py @@ -7,6 +7,15 @@ def is_prime(number: int) -> bool: + """ + Check whether a given integer is a prime number. + + :param n: integer to check + :return: True if n is prime, False otherwise + :raises ValueError: if n is less than 2 + """ + if n < 2: + raise ValueError("n must be an integer greater than or equal to 2") """Checks to see if a number is a prime in O(sqrt(n)). A number is prime if it has exactly two factors: 1 and itself. From 241f9c8b6dc16aaea95ff842505564ab3ff4b546 Mon Sep 17 00:00:00 2001 From: dhruvakidadumbe Date: Mon, 29 Dec 2025 17:07:33 +0530 Subject: [PATCH 7/8] Fix docstring and align prime check behavior with tests --- maths/prime_check.py | 57 ++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/maths/prime_check.py b/maths/prime_check.py index 445cf8cd8da1..15fb6cdc6b43 100644 --- a/maths/prime_check.py +++ b/maths/prime_check.py @@ -7,47 +7,30 @@ def is_prime(number: int) -> bool: - """ + """ Check whether a given integer is a prime number. - :param n: integer to check - :return: True if n is prime, False otherwise - :raises ValueError: if n is less than 2 - """ - if n < 2: - raise ValueError("n must be an integer greater than or equal to 2") - """Checks to see if a number is a prime in O(sqrt(n)). - - A number is prime if it has exactly two factors: 1 and itself. - - >>> is_prime(0) - False - >>> is_prime(1) - False - >>> is_prime(2) - True - >>> is_prime(3) - True - >>> is_prime(27) - False - >>> is_prime(87) - False - >>> is_prime(563) - True - >>> is_prime(2999) - True - >>> is_prime(67483) - False - >>> is_prime(16.1) - Traceback (most recent call last): - ... - ValueError: is_prime() only accepts positive integers - >>> is_prime(-4) - Traceback (most recent call last): - ... - ValueError: is_prime() only accepts positive integers + A prime number has exactly two distinct positive divisors: 1 and itself. + + :param number: integer to check + :return: True if number is prime, False otherwise + :raises ValueError: if input is not a positive integer """ + # precondition + if not isinstance(number, int) or number < 0: + raise ValueError("is_prime() only accepts positive integers") + + if 1 < number < 4: + return True + if number < 2 or number % 2 == 0 or number % 3 == 0: + return False + + for i in range(5, int(math.sqrt(number)) + 1, 6): + if number % i == 0 or number % (i + 2) == 0: + return False + return True + # precondition if not isinstance(number, int) or not number >= 0: raise ValueError("is_prime() only accepts positive integers") From 0e98f1a22597e6cba6d1e0473089fc3f7902e5eb Mon Sep 17 00:00:00 2001 From: dhruvakidadumbe Date: Mon, 29 Dec 2025 23:00:14 +0530 Subject: [PATCH 8/8] Improve docstring clarity for anagram check function --- strings/check_anagrams.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/strings/check_anagrams.py b/strings/check_anagrams.py index d747368b2373..03e2c50c3304 100644 --- a/strings/check_anagrams.py +++ b/strings/check_anagrams.py @@ -7,8 +7,15 @@ def check_anagrams(first_str: str, second_str: str) -> bool: """ - Two strings are anagrams if they are made up of the same letters but are - arranged differently (ignoring the case). + Check whether two strings are anagrams of each other. + + Two strings are anagrams if they contain the same characters + with the same frequency, ignoring case and whitespace. + + :param first_str: first input string + :param second_str: second input string + :return: True if strings are anagrams, False otherwise + >>> check_anagrams('Silent', 'Listen') True >>> check_anagrams('This is a string', 'Is this a string')