From b14e0145ac8cb48f397f48e0ed81796bd29bb929 Mon Sep 17 00:00:00 2001 From: Bharadwaj Date: Sat, 22 Oct 2016 04:59:14 +0000 Subject: [PATCH] Added arrays.includes method --- CHANGELOG.rst | 1 + README.rst | 15 +++++++++++++++ sugar/__init__.py | 1 + sugar/arrays.py | 36 ++++++++++++++++++++++++++++++++++++ tests/test_arrays.py | 12 ++++++++++++ 5 files changed, 65 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 60bfffa..10e56d3 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,6 +11,7 @@ Changelog - Add ``exclude()``. - Add ``filter()``. - Add ``first()``. +- Add ``includes()``. - Add ``is_array()``. - Add ``is_boolean()``. - Add ``is_number()``. diff --git a/README.rst b/README.rst index 5771ad6..c1326f5 100644 --- a/README.rst +++ b/README.rst @@ -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]) diff --git a/sugar/__init__.py b/sugar/__init__.py index 647e8a9..a62b59b 100644 --- a/sugar/__init__.py +++ b/sugar/__init__.py @@ -24,6 +24,7 @@ exclude, filter, first, + includes, subtract ) diff --git a/sugar/arrays.py b/sugar/arrays.py index b1093bb..111906f 100644 --- a/sugar/arrays.py +++ b/sugar/arrays.py @@ -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 diff --git a/tests/test_arrays.py b/tests/test_arrays.py index e93f51c..be0c4ba 100644 --- a/tests/test_arrays.py +++ b/tests/test_arrays.py @@ -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]),