From f0344507a6b82967c9dc8f5c0c3d9dfc13917482 Mon Sep 17 00:00:00 2001 From: Bharadwaj Date: Tue, 11 Oct 2016 21:18:33 +0000 Subject: [PATCH] Added every() method --- CHANGELOG.rst | 5 +++-- README.rst | 5 +++++ sugar/__init__.py | 1 + sugar/arrays.py | 25 +++++++++++++++++++++++++ tests/test_arrays.py | 10 ++++++++++ 5 files changed, 44 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 67b7e31..68b851e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,8 +2,9 @@ Changelog ========= -- Add ``clone()`` method. -- Add ``compact()`` method. +- Add ``clone()``. +- Add ``compact()``. +- Add ``every()``. v0.1.1 (2016-10-10) diff --git a/README.rst b/README.rst index 24a562e..dc3cc39 100644 --- a/README.rst +++ b/README.rst @@ -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]) diff --git a/sugar/__init__.py b/sugar/__init__.py index bcb2b7f..29b1a1d 100644 --- a/sugar/__init__.py +++ b/sugar/__init__.py @@ -17,5 +17,6 @@ compact, construct, count, + every, subtract ) diff --git a/sugar/arrays.py b/sugar/arrays.py index 4f4b457..3cb389c 100644 --- a/sugar/arrays.py +++ b/sugar/arrays.py @@ -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 diff --git a/tests/test_arrays.py b/tests/test_arrays.py index a238f05..285bf03 100644 --- a/tests/test_arrays.py +++ b/tests/test_arrays.py @@ -8,6 +8,7 @@ compact, construct, count, + every, subtract ) @@ -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]),