Skip to content

Commit ec022c7

Browse files
realDuYuanChaogithub-actions
andauthored
Development (#22)
* add addition add subtract add count_digits * count digits recursion * cube number * add factorial add factorial_recursion * Formatted with psf/black * fixed build Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent a428f5b commit ec022c7

File tree

7 files changed

+158
-0
lines changed

7 files changed

+158
-0
lines changed

maths/addition.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def add(first_number, second_number):
2+
"""
3+
>>> add(1, 2)
4+
3
5+
>>> add(1.1, 2.2)
6+
3.3000000000000003
7+
>>> add(-1.1, 1.1)
8+
0.0
9+
"""
10+
return first_number + second_number
11+
12+
13+
if __name__ == "if __name__ == ''":
14+
from doctest import testmod
15+
16+
testmod()

maths/count_digits.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def count_digits(number: int) -> int:
2+
"""
3+
>>> count_digits(-123)
4+
3
5+
>>> count_digits(-1)
6+
1
7+
>>> count_digits(0)
8+
1
9+
>>> count_digits(123)
10+
3
11+
>>> count_digits(123456)
12+
6
13+
"""
14+
number = abs(number)
15+
count = 0
16+
while True:
17+
number = number // 10
18+
count = count + 1
19+
if number == 0:
20+
break
21+
return count
22+
23+
24+
if __name__ == "__main__":
25+
from doctest import testmod
26+
27+
testmod()

maths/count_digits_recursion.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def count_digits_recursion(number: int) -> int:
2+
"""
3+
>>> count_digits_recursion(-123)
4+
3
5+
>>> count_digits_recursion(-1)
6+
1
7+
>>> count_digits_recursion(0)
8+
1
9+
>>> count_digits_recursion(123)
10+
3
11+
>>> count_digits_recursion(123456)
12+
6
13+
"""
14+
number = abs(number)
15+
return 1 if number < 10 else 1 + count_digits_recursion(number // 10)
16+
17+
18+
if __name__ == "__main__":
19+
from doctest import testmod
20+
21+
testmod()

maths/cube_number.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
https://en.wikipedia.org/wiki/Cube_(algebra)
3+
"""
4+
5+
6+
def is_cube_number(number: int) -> bool:
7+
"""
8+
>>> all(is_cube_number(num) for num in [-8, -1, 0, 1, 8, 27, 64, 8000, 216_000])
9+
True
10+
>>> is_cube_number(4)
11+
False
12+
>>> is_cube_number(11)
13+
False
14+
"""
15+
number = abs(number)
16+
for i in range(0, number + 1):
17+
if i ** 3 == number:
18+
return True
19+
return False
20+
21+
22+
if __name__ == "__main__":
23+
from doctest import testmod
24+
25+
testmod()

maths/factorial.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def factorial(number: int) -> int:
2+
"""
3+
>>> factorial(5)
4+
120
5+
>>> factorial(0)
6+
1
7+
>>> import random
8+
>>> import math
9+
>>> numbers = list(range(0, 50))
10+
>>> for num in numbers:
11+
... assert factorial(num) == math.factorial(num)
12+
>>> factorial(-1)
13+
Traceback (most recent call last):
14+
...
15+
ValueError: factorial() not defined for negative values
16+
"""
17+
if number < 0:
18+
raise ValueError("factorial() not defined for negative values")
19+
fact = 1
20+
for i in range(1, number + 1):
21+
fact *= i
22+
return fact
23+
24+
25+
if __name__ == "__main__":
26+
from doctest import testmod
27+
28+
testmod()

maths/factorial_recursion.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def factorial_recursion(number: int) -> int:
2+
"""
3+
>>> factorial_recursion(5)
4+
120
5+
>>> factorial_recursion(0)
6+
1
7+
>>> import random
8+
>>> import math
9+
>>> numbers = list(range(0, 50))
10+
>>> for num in numbers:
11+
... assert factorial_recursion(num) == math.factorial(num)
12+
>>> factorial_recursion(-1)
13+
Traceback (most recent call last):
14+
...
15+
ValueError: factorial() not defined for negative values
16+
"""
17+
if number < 0:
18+
raise ValueError("factorial() not defined for negative values")
19+
return 1 if number == 0 or number == 1 else number * factorial_recursion(number - 1)
20+
21+
22+
if __name__ == "__main__":
23+
from doctest import testmod
24+
25+
testmod()

maths/subtract.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def subtract(first_number, second_number):
2+
"""
3+
>>> subtract(1.1, 1.1)
4+
0.0
5+
>>> subtract(10, 5)
6+
5
7+
>>> subtract(3.14159, 3)
8+
0.14158999999999988
9+
"""
10+
return first_number - second_number
11+
12+
13+
if __name__ == "__main__":
14+
from doctest import testmod
15+
16+
testmod()

0 commit comments

Comments
 (0)