-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_module.py
64 lines (50 loc) · 3.33 KB
/
test_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import unittest
import mean_var_std
import numpy as np
def calculate(numbers):
if len(numbers) != 9:
raise ValueError("List must contain nine numbers.")
# Convert the input list into a 3x3 NumPy array
matrix = np.array(numbers).reshape(3, 3)
# Calculate mean, variance, standard deviation, max, min, and sum
mean_axis1 = matrix.mean(axis=0).tolist()
mean_axis2 = matrix.mean(axis=1).tolist()
mean_flattened = matrix.mean().tolist()
variance_axis1 = matrix.var(axis=0).tolist()
variance_axis2 = matrix.var(axis=1).tolist()
variance_flattened = matrix.var().tolist()
std_deviation_axis1 = matrix.std(axis=0).tolist()
std_deviation_axis2 = matrix.std(axis=1).tolist()
std_deviation_flattened = matrix.std().tolist()
max_axis1 = matrix.max(axis=0).tolist()
max_axis2 = matrix.max(axis=1).tolist()
max_flattened = matrix.max().tolist()
min_axis1 = matrix.min(axis=0).tolist()
min_axis2 = matrix.min(axis=1).tolist()
min_flattened = matrix.min().tolist()
sum_axis1 = matrix.sum(axis=0).tolist()
sum_axis2 = matrix.sum(axis=1).tolist()
sum_flattened = matrix.sum().tolist()
result = {
'mean': [mean_axis1, mean_axis2, mean_flattened],
'variance': [variance_axis1, variance_axis2, variance_flattened],
'standard deviation': [std_deviation_axis1, std_deviation_axis2, std_deviation_flattened],
'max': [max_axis1, max_axis2, max_flattened],
'min': [min_axis1, min_axis2, min_flattened],
'sum': [sum_axis1, sum_axis2, sum_flattened]
}
return result
# the test case
class UnitTests(unittest.TestCase):
def test_calculate(self):
actual = mean_var_std.calculate([2,6,2,8,4,0,1,5,7])
expected = {'mean': [[3.6666666666666665, 5.0, 3.0], [3.3333333333333335, 4.0, 4.333333333333333], 3.888888888888889], 'variance': [[9.555555555555557, 0.6666666666666666, 8.666666666666666], [3.555555555555556, 10.666666666666666, 6.222222222222221], 6.987654320987654], 'standard deviation': [[3.091206165165235, 0.816496580927726, 2.943920288775949], [1.8856180831641267, 3.265986323710904, 2.494438257849294], 2.6434171674156266], 'max': [[8, 6, 7], [6, 8, 7], 8], 'min': [[1, 4, 0], [2, 0, 1], 0], 'sum': [[11, 15, 9], [10, 12, 13], 35]}
self.assertAlmostEqual(actual, expected, "Expected different output when calling 'calculate()' with '[2,6,2,8,4,0,1,5,7]'")
def test_calculate2(self):
actual = mean_var_std.calculate([9,1,5,3,3,3,2,9,0])
expected = {'mean': [[4.666666666666667, 4.333333333333333, 2.6666666666666665], [5.0, 3.0, 3.6666666666666665], 3.888888888888889], 'variance': [[9.555555555555555, 11.555555555555557, 4.222222222222222], [10.666666666666666, 0.0, 14.888888888888891], 9.209876543209875], 'standard deviation': [[3.0912061651652345, 3.39934634239519, 2.0548046676563256], [3.265986323710904, 0.0, 3.8586123009300755], 3.0347778408328137], 'max': [[9, 9, 5], [9, 3, 9], 9], 'min': [[2, 1, 0], [1, 3, 0], 0], 'sum': [[14, 13, 8], [15, 9, 11], 35]}
self.assertAlmostEqual(actual, expected, "Expected different output when calling 'calculate()' with '[9,1,5,3,3,3,2,9,0]'")
def test_calculate_with_few_digits(self):
self.assertRaisesRegex(ValueError, "List must contain nine numbers.", mean_var_std.calculate, [2,6,2,8,4,0,1,])
if __name__ == "__main__":
unittest.main()