Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions maths/factorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
52 changes: 22 additions & 30 deletions maths/prime_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 9 additions & 2 deletions strings/check_anagrams.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions strings/palindrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down