From 790a354c098ea21cefd7819037a91473d7173968 Mon Sep 17 00:00:00 2001 From: shellhub Date: Tue, 3 Nov 2020 17:39:50 +0800 Subject: [PATCH 1/6] add addition add subtract add count_digits --- maths/addition.py | 16 ++++++++++++++++ maths/count_digits.py | 27 +++++++++++++++++++++++++++ maths/subtract.py | 16 ++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 maths/addition.py create mode 100644 maths/count_digits.py create mode 100644 maths/subtract.py diff --git a/maths/addition.py b/maths/addition.py new file mode 100644 index 0000000..2841758 --- /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.3 + >>> 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/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() From ee9ce1964b76120ccd5fb7280247e3c48bd69a72 Mon Sep 17 00:00:00 2001 From: shellhub Date: Tue, 3 Nov 2020 17:46:19 +0800 Subject: [PATCH 2/6] count digits recursion --- maths/count_digits_recursion.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 maths/count_digits_recursion.py 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() From 4333a59216ae3b1d3ddb5a2ba740136b7970f119 Mon Sep 17 00:00:00 2001 From: shellhub Date: Tue, 3 Nov 2020 17:52:31 +0800 Subject: [PATCH 3/6] cube number --- maths/cube_number.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 maths/cube_number.py diff --git a/maths/cube_number.py b/maths/cube_number.py new file mode 100644 index 0000000..ca57e56 --- /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() From 1637656a072550baf6e01b617b372ac37ad953df Mon Sep 17 00:00:00 2001 From: shellhub Date: Wed, 4 Nov 2020 08:31:05 +0800 Subject: [PATCH 4/6] add factorial add factorial_recursion --- maths/factorial.py | 28 ++++++++++++++++++++++++++++ maths/factorial_recursion.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 maths/factorial.py create mode 100644 maths/factorial_recursion.py diff --git a/maths/factorial.py b/maths/factorial.py new file mode 100644 index 0000000..ef1697b --- /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_recursion(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..ea731f4 --- /dev/null +++ b/maths/factorial_recursion.py @@ -0,0 +1,28 @@ +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() From c28f9023c94e3f7fc06911d5238503810340feac Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 4 Nov 2020 00:35:52 +0000 Subject: [PATCH 5/6] Formatted with psf/black --- maths/cube_number.py | 2 +- maths/factorial.py | 2 +- maths/factorial_recursion.py | 7 ++----- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/maths/cube_number.py b/maths/cube_number.py index ca57e56..b15dc12 100644 --- a/maths/cube_number.py +++ b/maths/cube_number.py @@ -19,7 +19,7 @@ def is_cube_number(number: int) -> bool: return False -if __name__ == '__main__': +if __name__ == "__main__": from doctest import testmod testmod() diff --git a/maths/factorial.py b/maths/factorial.py index ef1697b..32d1fad 100644 --- a/maths/factorial.py +++ b/maths/factorial.py @@ -22,7 +22,7 @@ def factorial(number: int) -> int: return fact -if __name__ == '__main__': +if __name__ == "__main__": from doctest import testmod testmod() diff --git a/maths/factorial_recursion.py b/maths/factorial_recursion.py index ea731f4..195b7ab 100644 --- a/maths/factorial_recursion.py +++ b/maths/factorial_recursion.py @@ -16,13 +16,10 @@ def factorial_recursion(number: int) -> int: """ 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) - ) + return 1 if number == 0 or number == 1 else number * factorial_recursion(number - 1) -if __name__ == '__main__': +if __name__ == "__main__": from doctest import testmod testmod() From 46ac441a8cce437e1ae2b2de1f1178064cd21036 Mon Sep 17 00:00:00 2001 From: shellhub Date: Wed, 4 Nov 2020 08:40:06 +0800 Subject: [PATCH 6/6] fixed build --- maths/addition.py | 2 +- maths/factorial.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/addition.py b/maths/addition.py index 2841758..d9b4231 100644 --- a/maths/addition.py +++ b/maths/addition.py @@ -3,7 +3,7 @@ def add(first_number, second_number): >>> add(1, 2) 3 >>> add(1.1, 2.2) - 3.3 + 3.3000000000000003 >>> add(-1.1, 1.1) 0.0 """ diff --git a/maths/factorial.py b/maths/factorial.py index 32d1fad..af8b17e 100644 --- a/maths/factorial.py +++ b/maths/factorial.py @@ -8,7 +8,7 @@ def factorial(number: int) -> int: >>> import math >>> numbers = list(range(0, 50)) >>> for num in numbers: - ... assert factorial(num) == math.factorial_recursion(num) + ... assert factorial(num) == math.factorial(num) >>> factorial(-1) Traceback (most recent call last): ...