Mean-Variance-Standard Deviation Calculator Challenge

Tell us what’s happening:
Hello everyone , my code is not passing the automated test. But looking at the results I am pretty sure I got the desired outcome.

What should I do to fix and pass the automated test ?

My code so far:

import numpy as np

def calculate(list):

  if len(list) < 9:
    raise ValueError('List must contain nine numbers.')

  transformedList = np.array(list).reshape(3,3)


  transformedListSum = transformedList.sum().tolist()
  transformedListMean = transformedList.mean().tolist()
  transformedListMax = transformedList.max().tolist()
  transformedListMin = transformedList.min().tolist()
  transformedListStdDeviation = transformedList.std().tolist()
  transformedListVariance = transformedList.var().tolist()

  transformedListSumAxis1 = transformedList.sum(axis=1).tolist()
  transformedListMeanAxis1 = transformedList.mean(axis=1).tolist()
  transformedListMaxAxis1 = transformedList.max(axis=1).tolist()
  transformedListMinAxis1 = transformedList.min(axis=1).tolist()
  transformedListStdDeviationAxis1 = transformedList.std(axis=1).tolist()
  transformedListVarianceAxis1 = transformedList.var(axis=1).tolist()

  transformedListSumAxis0 = transformedList.sum(axis=0).tolist()
  transformedListMeanAxis0 = transformedList.mean(axis=0).tolist()
  transformedListMaxAxis0 = transformedList.max(axis=0).tolist()
  transformedListMinAxis0 = transformedList.min(axis=0).tolist()
  transformedListStdDeviationAxis0 = transformedList.std(axis=0).tolist()
  transformedListVarianceAxis0 = transformedList.var(axis=0).tolist()

  calculations = {
  'mean': [transformedListMeanAxis0,transformedListMeanAxis1,transformedListMean],
  'variance': [transformedListVarianceAxis0,transformedListVarianceAxis1,transformedListVariance],
  'standard deviation': [transformedListStdDeviationAxis0,transformedListStdDeviationAxis1,transformedListStdDeviation],
  'max': [transformedListMaxAxis0,transformedListMaxAxis1,transformedListMax],
  'min': [transformedListMinAxis0,transformedListMinAxis1,transformedListMin],
  'sum': [transformedListSumAxis0,transformedListSumAxis1,transformedListSum]
}


  return print(calculations)

calculate([9,1,5,3,3,3,2,9,0])

Challenge: Mean-Variance-Standard Deviation Calculator

Link to the challenge:

Please post your error messages. Thanks.

Hello Jeremy,

This is what I got when running the code :

 python main.py
{'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]}
{'mean': [[3.0, 4.0, 5.0], [1.0, 4.0, 7.0], 4.0], 'variance': [[6.0, 6.0, 6.0], [0.6666666666666666, 0.6666666666666666, 0.6666666666666666], 6.666666666666667], 'standard deviation': [[
 python main.py
{'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]}
{'mean': [[3.0, 4.0, 5.0], [1.0, 4.0, 7.0], 4.0], 'variance': [[6.0, 6.0, 6.0], [0.6666666666
 python main.py
{'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]}
{'mean': [[3.0, 4.0, 5.0], [1.0, 4.0, 7.0], 4.0], 'variance': [[6.0, 6.0, 6.0], [0.6666666666666666, 0.6666666666666666, 0.6666666666666666], 6.666666666666667], 'standard deviation': [[2.449489742783178, 2.449489742783178, 2.449489742783178], [0.816496580927726, 0.816496580927726, 0.816496580927726], 2.581988897471611], 'max': [[6, 7, 8], [2, 5, 8], 8], 'min': [[0, 1, 2], [0, 3, 6], 0], 'sum': [[9, 12, 15], [3, 12, 21], 36]}
None
{'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]}
E{'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]}
E.
======================================================================
ERROR: test_calculate (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator-2/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 -: 'NoneType' and 'dict'

======================================================================
ERROR: test_calculate2 (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-mean-variance-standard-deviation-calculator-2/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 -: 'NoneType' and 'dict'

----------------------------------------------------------------------
Ran 3 tests in 0.003s

FAILED (errors=2)
 

I think I found out what the problem is.

I was saving the dictionary to a variable called calculations and then returning that variable.

I changed it and now I only return the dictionary WITHOUT saving it to a variable.

Before:

  calculations = {
  'mean': [transformedListMeanAxis0,transformedListMeanAxis1,transformedListMean],
  'variance': [transformedListVarianceAxis0,transformedListVarianceAxis1,transformedListVariance],
  'standard deviation': [transformedListStdDeviationAxis0,transformedListStdDeviationAxis1,transformedListStdDeviation],
  'max': [transformedListMaxAxis0,transformedListMaxAxis1,transformedListMax],
  'min': [transformedListMinAxis0,transformedListMinAxis1,transformedListMin],
  'sum': [transformedListSumAxis0,transformedListSumAxis1,transformedListSum]
}

After:

  return  {
  'mean': [transformedListMeanAxis0,transformedListMeanAxis1,transformedListMean],
  'variance': [transformedListVarianceAxis0,transformedListVarianceAxis1,transformedListVariance],
  'standard deviation': [transformedListStdDeviationAxis0,transformedListStdDeviationAxis1,transformedListStdDeviation],
  'max': [transformedListMaxAxis0,transformedListMaxAxis1,transformedListMax],
  'min': [transformedListMinAxis0,transformedListMinAxis1,transformedListMin],
  'sum': [transformedListSumAxis0,transformedListSumAxis1,transformedListSum]
}