Skip to content

Commit

Permalink
Added exclude() method
Browse files Browse the repository at this point in the history
  • Loading branch information
bharadwajyarlagadda committed Oct 11, 2016
1 parent f034450 commit b2db403
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
- Add ``clone()``.
- Add ``compact()``.
- Add ``every()``.
- Add ``exclude()``.


v0.1.1 (2016-10-10)
Expand Down
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ Install using pip:
>>> every([2, 2, 3], 2)
False
>>> exclude([11, 22, 33], 22)
[11, 33]
>>> exclude([11, 22, 33], 44)
[11, 22, 33]
>>> exclude([11, 22, 33], [11, 22])
[33]
>>> 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 @@ -18,5 +18,6 @@
construct,
count,
every,
exclude,
subtract
)
25 changes: 25 additions & 0 deletions sugar/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,31 @@ def every(array, value):
return all(element == value for element in array)


def exclude(array, value):
"""Returns a new array with every element that does not match
:attr:`value`.
Args:
array(list): List of values provided by the user.
value(int/float/str): A value that needs to be excluded.
Returns:
list: List excluding the give :attr:`value`.
Example:
>>> exclude([11, 22, 33], 22)
[11, 33]
>>> exclude([11, 22, 33], 44)
[11, 22, 33]
>>> exclude([11, 22, 33], [11, 22])
[33]
.. versionadded:: 0.2.0-dev
"""
return subtract(array, value)


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
11 changes: 11 additions & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
construct,
count,
every,
exclude,
subtract
)

Expand Down Expand Up @@ -75,6 +76,16 @@ def test_every(array, value, expected_output):
assert every(array, value) == expected_output


@parametrize('array,item,expected_output', [
([1, 2, 3], 3, [1, 2]),
([1, 2, 3], [1, 3], [2]),
([1, 2, 3], 4, [1, 2, 3])
])
def test_exclude(array, item, expected_output):
"""Tests whether the exclude method is working properly or not."""
assert exclude(array, item) == 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 b2db403

Please sign in to comment.