|
|
|
|
|
by notaddicted
4819 days ago
|
|
Also available in python: import heapq
n = 2
heapq.nlargest(n, vals)[n-1]
Edit: and out of morbid curiosity, here is the same implemented by a scan. It could be arranged as a single statement. swapIfGt = lambda xs, q: xs if xs[0] >= q else sorted([q]+xs[1:])
n = 2
reduce(swapIfGt, vals, [None]*n)[0]
Edit2: rewrote swapIfGt |
|