Skip to content

Commit

Permalink
Added average method
Browse files Browse the repository at this point in the history
  • Loading branch information
bharadwajyarlagadda committed Oct 10, 2016
1 parent 8db36e2 commit 658de14
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ Changelog


- First release.
- Add ``average()``.
- Add ``construct()``.
- Add ``count()``.
1 change: 1 addition & 0 deletions sugar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)

from .arrays import (
average,
construct,
count
)
19 changes: 19 additions & 0 deletions sugar/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
9 changes: 9 additions & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .fixtures import parametrize

from sugar import (
average,
construct,
count
)
Expand All @@ -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])
Expand Down

0 comments on commit 658de14

Please sign in to comment.