diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 0bb8bf8..69fcb06 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,5 +3,6 @@ Changelog - First release. +- Add ``average()``. - Add ``construct()``. - Add ``count()``. diff --git a/sugar/__init__.py b/sugar/__init__.py index c2d126d..2c3fcdf 100644 --- a/sugar/__init__.py +++ b/sugar/__init__.py @@ -12,6 +12,7 @@ ) from .arrays import ( + average, construct, count ) diff --git a/sugar/arrays.py b/sugar/arrays.py index 8be5ee5..204871d 100644 --- a/sugar/arrays.py +++ b/sugar/arrays.py @@ -3,6 +3,25 @@ from __future__ import absolute_import +def average(array): + """Returns the average for all the values in the given :attr:`array`. + + Args: + array (list): List of values. + + Returns: + int/float: Average of all the values in the given list. + + Example: + + >>> average([1, 2, 3]) + 2.0 + + .. versionadded:: 0.1.0-dev + """ + return sum(array) / len(array) + + def construct(var, callback): """Constructs an array of :attr:`var` length from the values of :attr:`callback`. diff --git a/tests/test_arrays.py b/tests/test_arrays.py index bd7f233..012e9c0 100644 --- a/tests/test_arrays.py +++ b/tests/test_arrays.py @@ -3,6 +3,7 @@ from .fixtures import parametrize from sugar import ( + average, construct, count ) @@ -18,6 +19,14 @@ def triple(variable): return variable * 3 +@parametrize('array,expected_average', [ + ([1, 2, 3], 2) +]) +def test_average(array, expected_average): + """Tests whether the average method is working properly or not.""" + assert average(array) == expected_average + + @parametrize('length,callback,expected_output', [ (4, double, [0, 2, 4, 6]), (4, triple, [0, 3, 6, 9])