Skip to content

Commit

Permalink
Added every() method
Browse files Browse the repository at this point in the history
  • Loading branch information
bharadwajyarlagadda committed Oct 11, 2016
1 parent a144520 commit f034450
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ Changelog
=========


- Add ``clone()`` method.
- Add ``compact()`` method.
- Add ``clone()``.
- Add ``compact()``.
- Add ``every()``.


v0.1.1 (2016-10-10)
Expand Down
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ Install using pip:
>>> sugar.count([1, 2, 3, 3], 3)
2
>>> every([2, 2, 2], 2)
True
>>> every([2, 2, 3], 2)
False
>>> sugar.subtract([1, 2, 3], 2)
[1, 3]
>>> sugar.subtract ([1, 2, 3], [1, 3])
Expand Down
1 change: 1 addition & 0 deletions sugar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
compact,
construct,
count,
every,
subtract
)
25 changes: 25 additions & 0 deletions sugar/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,31 @@ def count(array, value):
return array.count(value)


def every(array, value):
"""Returns true if search is true for all elements of the array. In other
words, this method returns True if :attr:`array` contains all the same
values :attr:`value`.
Args:
array (list): List of values provided by the user.
value (int/float/str): Value that needs to be searched.
Returns:
bool: A boolean value based on the :attr:`array` having all the
values as :attr:`value`.
Example:
>>> every([2, 2, 2], 2)
True
>>> every([2, 2, 3], 2)
False
.. versionadded:: 0.2.0-dev
"""
return all(element == value for element in array)


def subtract(array, item):
"""Subtracts :attr:`item` from the :attr:`array` and returns the result
as a new array. If :attr:`item` is also an array, all elements in it will
Expand Down
10 changes: 10 additions & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
compact,
construct,
count,
every,
subtract
)

Expand Down Expand Up @@ -65,6 +66,15 @@ def test_count(array, value, expected_output):
assert count(array, value) == expected_output


@parametrize('array,value,expected_output', [
([2, 2, 2], 2, True),
([2, 3, 4], 2, False)
])
def test_every(array, value, expected_output):
"""Tests whether the every method is working properly or not."""
assert every(array, value) == expected_output


@parametrize('array,item,expected_output', [
([1, 2, 3], 3, [1, 2]),
([1, 2, 3], [1, 3], [2]),
Expand Down

0 comments on commit f034450

Please sign in to comment.