Skip to content

Commit f93cce6

Browse files
engFelipeMonteirocclauss
authored andcommitted
some pytest on math folder (TheAlgorithms#1405)
* some pytest on math folder * Run the test function via a doctest Also format the code with psf/black as discussed in CONTRIBUTING.md * Update abs.py * Update average_mean.py
1 parent 67aa3cf commit f93cce6

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

maths/3n+1.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ def n31(a: int) -> Tuple[List[int], int]:
2323
return path, len(path)
2424

2525

26-
def main():
27-
num = 4
28-
path, length = n31(num)
29-
print(
30-
"The Collatz sequence of {0} took {1} steps. \nPath: {2}".format(
31-
num, length, path
32-
)
33-
)
26+
def test_n31():
27+
"""
28+
>>> test_n31()
29+
"""
30+
assert n31(4) == ([4, 2, 1], 3)
31+
assert n31(11) == ([11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1], 15)
32+
assert n31(31) == ([31, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1], 107)
3433

3534

3635
if __name__ == "__main__":
37-
main()
36+
num = 4
37+
path, length = n31(num)
38+
print(f"The Collatz sequence of {num} took {length} steps. \nPath: {path}")

maths/abs.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@ def abs_val(num):
55
"""
66
Find the absolute value of a number.
77
8-
>>abs_val(-5)
9-
5
10-
>>abs_val(0)
8+
>>> abs_val(-5.1)
9+
5.1
10+
>>> abs_val(-5) == abs_val(5)
11+
True
12+
>>> abs_val(0)
1113
0
1214
"""
1315
return -num if num < 0 else num
1416

1517

16-
def main():
17-
"""Print absolute value of -34."""
18-
print(abs_val(-34)) # = 34
18+
def test_abs_val():
19+
"""
20+
>>> test_abs_val()
21+
"""
22+
assert 0 == abs_val(0)
23+
assert 34 == abs_val(34)
24+
assert 100000000000 == abs_val(-100000000000)
1925

2026

2127
if __name__ == "__main__":
22-
main()
28+
print(abs_val(-34)) # --> 34

maths/average_mean.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33

44
def average(nums):
55
"""Find mean of a list of numbers."""
6-
avg = sum(nums) / len(nums)
7-
return avg
6+
return sum(nums) / len(nums)
87

98

10-
def main():
11-
"""Call average module to find mean of a specific list of numbers."""
12-
print(average([2, 4, 6, 8, 20, 50, 70]))
9+
def test_average():
10+
"""
11+
>>> test_average()
12+
"""
13+
assert 12.0 == average([3, 6, 9, 12, 15, 18, 21])
14+
assert 20 == average([5, 10, 15, 20, 25, 30, 35])
15+
assert 4.5 == average([1, 2, 3, 4, 5, 6, 7, 8])
1316

1417

1518
if __name__ == "__main__":
16-
main()
19+
"""Call average module to find mean of a specific list of numbers."""
20+
print(average([2, 4, 6, 8, 20, 50, 70]))

0 commit comments

Comments
 (0)