diff --git a/maths/addition.py b/maths/addition.py new file mode 100644 index 0000000..d9b4231 --- /dev/null +++ b/maths/addition.py @@ -0,0 +1,16 @@ +def add(first_number, second_number): + """ + >>> add(1, 2) + 3 + >>> add(1.1, 2.2) + 3.3000000000000003 + >>> add(-1.1, 1.1) + 0.0 + """ + return first_number + second_number + + +if __name__ == "if __name__ == ''": + from doctest import testmod + + testmod() diff --git a/maths/count_digits.py b/maths/count_digits.py new file mode 100644 index 0000000..d4ce2e3 --- /dev/null +++ b/maths/count_digits.py @@ -0,0 +1,27 @@ +def count_digits(number: int) -> int: + """ + >>> count_digits(-123) + 3 + >>> count_digits(-1) + 1 + >>> count_digits(0) + 1 + >>> count_digits(123) + 3 + >>> count_digits(123456) + 6 + """ + number = abs(number) + count = 0 + while True: + number = number // 10 + count = count + 1 + if number == 0: + break + return count + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/maths/count_digits_recursion.py b/maths/count_digits_recursion.py new file mode 100644 index 0000000..d23bd49 --- /dev/null +++ b/maths/count_digits_recursion.py @@ -0,0 +1,21 @@ +def count_digits_recursion(number: int) -> int: + """ + >>> count_digits_recursion(-123) + 3 + >>> count_digits_recursion(-1) + 1 + >>> count_digits_recursion(0) + 1 + >>> count_digits_recursion(123) + 3 + >>> count_digits_recursion(123456) + 6 + """ + number = abs(number) + return 1 if number < 10 else 1 + count_digits_recursion(number // 10) + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/maths/cube_number.py b/maths/cube_number.py new file mode 100644 index 0000000..b15dc12 --- /dev/null +++ b/maths/cube_number.py @@ -0,0 +1,25 @@ +""" +https://en.wikipedia.org/wiki/Cube_(algebra) +""" + + +def is_cube_number(number: int) -> bool: + """ + >>> all(is_cube_number(num) for num in [-8, -1, 0, 1, 8, 27, 64, 8000, 216_000]) + True + >>> is_cube_number(4) + False + >>> is_cube_number(11) + False + """ + number = abs(number) + for i in range(0, number + 1): + if i ** 3 == number: + return True + return False + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/maths/factorial.py b/maths/factorial.py new file mode 100644 index 0000000..af8b17e --- /dev/null +++ b/maths/factorial.py @@ -0,0 +1,28 @@ +def factorial(number: int) -> int: + """ + >>> factorial(5) + 120 + >>> factorial(0) + 1 + >>> import random + >>> import math + >>> numbers = list(range(0, 50)) + >>> for num in numbers: + ... assert factorial(num) == math.factorial(num) + >>> factorial(-1) + Traceback (most recent call last): + ... + ValueError: factorial() not defined for negative values + """ + if number < 0: + raise ValueError("factorial() not defined for negative values") + fact = 1 + for i in range(1, number + 1): + fact *= i + return fact + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/maths/factorial_recursion.py b/maths/factorial_recursion.py new file mode 100644 index 0000000..195b7ab --- /dev/null +++ b/maths/factorial_recursion.py @@ -0,0 +1,25 @@ +def factorial_recursion(number: int) -> int: + """ + >>> factorial_recursion(5) + 120 + >>> factorial_recursion(0) + 1 + >>> import random + >>> import math + >>> numbers = list(range(0, 50)) + >>> for num in numbers: + ... assert factorial_recursion(num) == math.factorial(num) + >>> factorial_recursion(-1) + Traceback (most recent call last): + ... + ValueError: factorial() not defined for negative values + """ + if number < 0: + raise ValueError("factorial() not defined for negative values") + return 1 if number == 0 or number == 1 else number * factorial_recursion(number - 1) + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/maths/subtract.py b/maths/subtract.py new file mode 100644 index 0000000..1aafd5b --- /dev/null +++ b/maths/subtract.py @@ -0,0 +1,16 @@ +def subtract(first_number, second_number): + """ + >>> subtract(1.1, 1.1) + 0.0 + >>> subtract(10, 5) + 5 + >>> subtract(3.14159, 3) + 0.14158999999999988 + """ + return first_number - second_number + + +if __name__ == "__main__": + from doctest import testmod + + testmod()