diff --git a/maths/factorial.py b/maths/factorial.py index ba61447c7564..9082ad241dc3 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!). - >>> 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 + 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") diff --git a/maths/prime_check.py b/maths/prime_check.py index a757c4108f24..15fb6cdc6b43 100644 --- a/maths/prime_check.py +++ b/maths/prime_check.py @@ -7,37 +7,29 @@ def is_prime(number: int) -> bool: - """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 """ + Check whether a given integer is a prime number. + + 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: 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') diff --git a/strings/palindrome.py b/strings/palindrome.py index e765207e5942..f4e88c516733 100644 --- a/strings/palindrome.py +++ b/strings/palindrome.py @@ -20,10 +20,10 @@ def is_palindrome(s: str) -> bool: """ - Return True if s is a palindrome otherwise return False. + Check whether a given string is a palindrome. - >>> all(is_palindrome(key) is value for key, value in test_data.items()) - True + :param s: input string + :return: True if palindrome, False otherwise """ start_i = 0