| Total Complexity | 5 |
| Total Lines | 42 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | #!/usr/bin/env python3 |
||
| 2 | 1 | """ |
|
| 3 | Matmod exercise 01 |
||
| 4 | |||
| 5 | ported to python by https://github.com/SpaceLenore |
||
| 6 | """ |
||
| 7 | 1 | import math |
|
| 8 | |||
| 9 | 1 | def mean(numbers): |
|
| 10 | """ |
||
| 11 | Calculate the mean of an array of numbers |
||
| 12 | """ |
||
| 13 | 1 | sumOfNumbers = 0 |
|
| 14 | 1 | for number in numbers: |
|
| 15 | 1 | sumOfNumbers += number |
|
| 16 | 1 | res = sumOfNumbers / len(numbers) |
|
| 17 | 1 | return round(res, 2) |
|
| 18 | |||
| 19 | 1 | def variance(numbers): |
|
| 20 | """ |
||
| 21 | Calculate the sample variance of an array of numbers |
||
| 22 | """ |
||
| 23 | 1 | xMean = mean(numbers) |
|
| 24 | 1 | totalSum = 0 |
|
| 25 | |||
| 26 | 1 | for x in numbers: |
|
| 27 | 1 | tmp = (x - xMean)**2 |
|
| 28 | 1 | totalSum += tmp |
|
| 29 | |||
| 30 | 1 | final = totalSum / (len(numbers) - 1) |
|
| 31 | |||
| 32 | |||
| 33 | 1 | return round(final, 2) |
|
| 34 | |||
| 35 | |||
| 36 | 1 | def stddev(numbers): |
|
| 37 | """ |
||
| 38 | Calculate the sample standard deviation of an array of numbers |
||
| 39 | """ |
||
| 40 | |||
| 41 | return round(math.sqrt(variance(numbers)), 2) |
||
| 42 |