Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added two methods to find the index / indices for the min and max value of a list #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/underscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,54 @@ def min(self):
return self._wrap(list())
return self._wrap(min(self.obj))

def list_min_index_value(self):
"""
Given a list, returns the index which has the minimum value, along with the value.
It assumes the values are comparable objects eg integers, strings, objects that implement __cmp__
:param values_list: A list to be searched upon
:return: A list of index(es) that have the minimum value
"""

if len(self.obj) == 0:
return self._wrap([])

min_value = min(self.obj)
how_many = self.obj.count(min_value)
c = -1
index_list = []
for idx, value in enumerate(self.obj):
if value == min_value:
index_list.append(idx)

if len(index_list) > how_many:
break

return self._wrap(index_list)

def list_max_index_value(self):
"""
Given a list, returns the index which has the maximum value, along with the value.
It assumes the values are comparable objects eg integers, strings, objects that implement __cmp__
:param values_list: A list to be searched upon
:return: A list of index(es) that have the maximum value
"""

if len(self.obj) == 0:
return self._wrap([])

max_value = max(self.obj)
how_many = self.obj.count(max_value)
index_list = []
for idx, value in enumerate(self.obj):
if value == max_value:
index_list.append(idx)

if len(index_list) >= how_many:
break


return self._wrap(index_list)

def shuffle(self):
""" Shuffle an array.
"""
Expand Down
31 changes: 31 additions & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,37 @@ def test_range(self):
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9], 'final'
' example in the Python docs')

def test_min_list(self):
res = _([-55, -55, -7, 15, 2, 1, -7, 4]).list_min_index_value()
expected = [0, 1]
assert res == expected

def test_min_empty_list(self):
res = _([]).list_min_index_value()
expected = []
assert res == expected

def test_min_one_value(self):
res = _([2, 2, 2, 2]).list_min_index_value()
expected = [0, 1, 2, 3]
assert res == expected

def test_max_list(self):
res = _([-55, -55, -7, 15, 2, 1, -7, 4]).list_max_index_value()
expected = [3]
assert res == expected

def test_max_empty_list(self):
res = _([]).list_max_index_value()
expected = []
assert res == expected

def test_max_one_value(self):
res = _([2, 2, 2, 2]).list_max_index_value()
expected = [0, 1, 2, 3]
assert res == expected


if __name__ == "__main__":
print("run these tests by executing `python -m unittest"
" discover` in unittests folder")
Expand Down