Skip to content

Commit

Permalink
Added arrays.includes method
Browse files Browse the repository at this point in the history
  • Loading branch information
bharadwajyarlagadda committed Oct 22, 2016
1 parent 87db84f commit b14e014
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Changelog
- Add ``exclude()``.
- Add ``filter()``.
- Add ``first()``.
- Add ``includes()``.
- Add ``is_array()``.
- Add ``is_boolean()``.
- Add ``is_number()``.
Expand Down
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ Install using pip:
>>> _.first([11, 22, 33, 44], 9)
[11, 22, 33, 44]
>>> _.includes([11, 22, 33], 22, 0)
True
>>> _.includes([11, 22, 33], 22, 1)
True
>>> _.includes([11, 22, 33], 22, 2)
False
>>> _.includes([11, 22, 33], 11, None)
True
>>> _.includes([11, 22, 33], 33)
True
>>> _.includes([11, 22, 33], 22, -1)
False
>>> _.includes([11, 22, 33], 22, -2)
True
>>> _.subtract([1, 2, 3], 2)
[1, 3]
>>> _.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 @@ -24,6 +24,7 @@
exclude,
filter,
first,
includes,
subtract
)

Expand Down
36 changes: 36 additions & 0 deletions sugar/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,42 @@ def first(array, num=1):
return array[0:num]


def includes(array, search, fromindex=0):
"""Returns true if search is contained within the array. Search begins at
fromindex, which defaults to the beginning of the array.
Args:
array (list): A list of values provided by the user.
search (mixed): A value that needs to be searched (provided by the
user).
fromindex (int): Search begins at fromindex.
Returns:
bool: True if search is contained within the array beginning at
fromindex position else False.
Example:
>>> includes([11, 22, 33], 22, 0)
True
>>> includes([11, 22, 33], 22, 1)
True
>>> includes([11, 22, 33], 22, 2)
False
>>> includes([11, 22, 33], 11, None)
True
>>> includes([11, 22, 33], 33)
True
>>> includes([11, 22, 33], 22, -1)
False
>>> includes([11, 22, 33], 22, -2)
True
.. versionadded:: TODO
"""
return search in array[fromindex:]


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
12 changes: 12 additions & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ def test_first(array, num, expected):
assert _.first(array, num) == expected


@parametrize('array,search,fromindex,expected', [
([11, 22, 33], 22, 1, True),
([11, 22, 33], 22, 2, False),
([11, 22, 33], 33, None, True),
([11, 22, 33], 22, 0, True),
([11, 22, 33], 55, None, False),
([11, 22, 33], 33, -1, True)
])
def test_includes(array, search, fromindex, expected):
assert _.includes(array, search, fromindex) == expected


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

0 comments on commit b14e014

Please sign in to comment.