Objective
The heapq module implements heaps (priority queues). With nlargest and nsmallest you get the extremes of a list without sorting it entirely.
Instructions
- Read an integer
kand a line with whole numbers (there are at leastk). - Compute the
klargest withheapq.nlargest(k, numbers): they come out from largest to smallest. - Compute the
ksmallest withheapq.nsmallest(k, numbers): they come out from smallest to largest. - Show
Largest: ...andSmallest: ..., with the values separated by a space.
2
5 1 9 3 7
Largest: 9 7
Smallest: 1 3
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise