Multiple maximums
Source: Python Morsels
Description
Return all maximum values in an iterable.
1
2
>>> multimax([1, 4, 2, 4, 3])
[4, 4]
Notes
Solution involved using using a lambda function as a keyword argument.
My solution
1
2
3
4
5
6
7
8
9
10
11
12
def multimax(iter, key=lambda x: x):
iter, max = list(iter), []
if not len(iter):
return max
max.append(iter[0])
for item in iter[1:]:
kitem, kmax = key(item), key(max[0])
if kitem > kmax:
max = [item]
elif kitem == kmax:
max.append(item)
return max
Provided solution
This solution does not invoke the key
function on the current
maximum value during each iteration, as mine does unnecessarily.
1
2
3
4
5
6
7
8
9
10
11
def multimax(iterable, key=lambda x: x):
max_key = None
maximums = []
for item in iterable:
k = key(item)
if k == max_key:
maximums.append(item)
elif not maximums or k > max_key:
maximums = [item]
max_key = k
return maximums