Hi, and thanks in advance for any help the community can offer.
Have written the following solution for this problem:
import numpy as np
def calculate(list):
if len(list) != 9:
raise ValueError('List must contain nine numbers.')
else:
a = list[:3]
b = list[3:6]
c = list[6:]
m1 = np.array([a, b, c])
calculations = {
'mean ': (np.mean(m1, axis=0).tolist(), np.mean(m1, axis=1).tolist(), np.mean(list)),
'variance ': (np.var(m1, axis=0).tolist(), np.var(m1, axis=1).tolist(), np.var(list)),
'standard deviation ': (np.std(m1, axis=0).tolist(), np.std(m1, axis=1).tolist(), np.std(list)),
'max ': (np.max(m1, axis=0).tolist(), np.max(m1, axis=1).tolist(), np.max(list)),
'min ': (np.min(m1, axis=0).tolist(), np.min(m1, axis=1).tolist(), np.min(list)),
'sum ': (np.sum(m1, axis=0).tolist(), np.sum(m1, axis=1).tolist(), np.sum(list))
}
return(calculations)
Which appears to do the job mostly, but I’m still getting the following 2 errors:
ERROR: test_calculate (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/ImperfectPepperyApplicationframework/test_module.py", line 10, in test_calculate
self.assertAlmostEqual(actual, expected, "Expected different output when calling 'calculate()' with '[2,6,2,8,4,0,1,5,7]'")
File "/usr/lib/python3.8/unittest/case.py", line 943, in assertAlmostEqual
diff = abs(first - second)
TypeError: unsupported operand type(s) for -: 'dict' and 'dict'
======================================================================
ERROR: test_calculate2 (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/ImperfectPepperyApplicationframework/test_module.py", line 15, in test_calculate2
self.assertAlmostEqual(actual, expected, "Expected different output when calling 'calculate()' with '[9,1,5,3,3,3,2,9,0]'")
File "/usr/lib/python3.8/unittest/case.py", line 943, in assertAlmostEqual
diff = abs(first - second)
TypeError: unsupported operand type(s) for -: 'dict' and 'dict'
----------------------------------------------------------------------
Can anyone help me understand what I’m doing wrong here? Can’t quite figure it out.
Thanks,
Hal