Created
March 16, 2023 09:07
-
-
Save marcusschiesser/d7ca03e6cbb14607778aa2a17b478071 to your computer and use it in GitHub Desktop.
Returns all items of an iterable with the same maximum or minimum value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_minimums(iterable, func): | |
"""Returns all items of the iterable with the same minimum value""" | |
minimum = None | |
minimums = [] | |
for item in iterable: | |
if minimum is None or func(item) < minimum: | |
minimum = func(item) | |
minimums = [item] | |
elif func(item) == minimum: | |
minimums.append(item) | |
return minimums | |
def find_maximums(iterable, func): | |
"""Returns all items of the iterable with the same maximum value""" | |
maximum = None | |
maximums = [] | |
for item in iterable: | |
if maximum is None or func(item) > maximum: | |
maximum = func(item) | |
maximums = [item] | |
elif func(item) == maximum: | |
maximums.append(item) | |
return maximums |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment