Sorting
Sorting is mostly about choosing the key, trusting stability, and knowing when a comparison sort is not needed at all. Two problems ask you to write a merge; the rest ask you to use sorted well.
The lesson is Sorting.
Sort by frequency
Write by_frequency(items) that returns the list sorted so the most frequent values
come first. Values with the same frequency are ordered smallest first, and all copies
of a value stay together.
by_frequency([4, 1, 2, 2, 3, 3, 3]) -> [3, 3, 3, 2, 2, 1, 4]
by_frequency([]) -> []
The point is to practise a key built from a Counter.
def by_frequency(items):
...
Merge three sorted lists
Write merge_three(a, b, c) that merges three sorted lists into one sorted list.
merge_three([1, 4], [2, 3], [0, 5]) -> [0, 1, 2, 3, 4, 5]
merge_three([], [], [7]) -> [7]
The point is to practise the merge step, so build it from merges rather than
concatenating and calling sorted. Merging the first two and then merging the result
with the third is fine.
def merge_three(a, b, c):
...
Count inversions
An inversion is a pair of positions i < j with items[i] > items[j]: two items that
are out of order relative to each other. Write count_inversions(items) that returns
how many there are. A sorted list has zero.
count_inversions([2, 4, 1, 3, 5]) -> 3 (2,1) (4,1) (4,3)
count_inversions([1, 2, 3]) -> 0
count_inversions([3, 2, 1]) -> 3
The point is to practise seeing that merge sort can count while it sorts: whenever the merge takes from the right half, every remaining item in the left half is an inversion with it. A pairwise loop also passes, but one check has twenty thousand items.
def count_inversions(items):
...
Largest number from pieces
nums is a list of non-negative whole numbers. Write largest_number(nums) that
arranges them, as strings joined together, to make the largest possible number, and
returns it as a string.
largest_number([10, 2]) -> "210"
largest_number([3, 30, 34, 5, 9]) -> "9534330"
largest_number([0, 0]) -> "0"
The point is to practise sorting with a custom notion of order. Comparing a + b
against b + a decides which of two pieces should come first.
def largest_number(nums):
...
Evens first
Write evens_first(items) that returns the list with all even numbers before all odd
numbers, keeping the original relative order within the evens and within the odds.
evens_first([3, 1, 2, 4, 5, 6]) -> [2, 4, 6, 3, 1, 5]
evens_first([1, 3]) -> [1, 3]
The point is to practise stable sorting by a boolean key.
def evens_first(items):
...
Sort by another list's order
Write relative_sort(items, order) that sorts items so values appear in the order
they have in order, and any values not in order come after, sorted ascending.
order has distinct values.
relative_sort([2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19], [2, 1, 4, 3, 9, 6])
-> [2, 2, 2, 1, 4, 3, 3, 9, 6, 7, 19]
The point is to practise a key that looks up a rank in a dictionary.
def relative_sort(items, order):
...
h-index
A researcher's h-index is the largest h such that at least h of their papers have
h or more citations each. Write h_index(citations) for a list of citation counts.
h_index([3, 0, 6, 1, 5]) -> 3 (three papers with at least 3 citations)
h_index([1, 3, 1]) -> 1
h_index([0]) -> 0
The point is to practise "sort, then one pass" where the pass compares each value with its position.
def h_index(citations):
...
Sort three colours in place
items contains only 0, 1 and 2. Write sort_colours(items) that rearranges
the list in place so all 0s come first, then 1s, then 2s. Return nothing; the
checks look at the list afterwards.
items = [2, 0, 2, 1, 1, 0]; sort_colours(items) -> items is now [0, 0, 1, 1, 2, 2]
The point is to practise sorting without sorted: count the three values and overwrite,
or use three pointers (the Dutch national flag method) for a single pass.
def sort_colours(items):
...
Top scores, ties by arrival
entries is a list of (name, score) in the order people finished. Write
top_k(entries, k) that returns the names of the k highest scores, highest first;
when scores tie, whoever appears earlier in entries comes first. k is at most the
number of entries.
top_k([("ana", 7), ("ben", 9), ("cy", 7), ("dee", 9)], 3) -> ["ben", "dee", "ana"]
The point is to practise relying on stability: sort by score only and let equal scores keep their input order.
def top_k(entries, k):
...
Pairs with the smallest difference
items is a list of at least two distinct whole numbers. Write closest_pairs(items)
that returns every pair [a, b] with a < b whose difference is the smallest difference
between any two values, ordered by a.
closest_pairs([4, 2, 1, 3]) -> [[1, 2], [2, 3], [3, 4]]
closest_pairs([1, 3, 6, 10, 15]) -> [[1, 3]]
The point is to practise "sort first": once sorted, the closest pairs are neighbours.
def closest_pairs(items):
...
Next: Hashing.