Drills
Five tasks. Each is one function in your algo project. We do not show
solutions; we show what you have to get.
All of them take a *metrics.Counter, and all of them must be deterministic:
the same call gives the same number.
1. LinearFindAll(items []Item, artist string, c *metrics.Counter) []int
Returns the indices of every item whose Artist matches.
How many comparisons does it use? Compare that with LinearFind. Why does the
number not depend on how many matches it found?
2. MinRating(items []Item, c *metrics.Counter) (Item, bool)
Returns the worst-rated item. false if the library is empty.
Count comparisons between ratings. For n items you must get exactly n−1. If you get n, you are comparing the first element with itself.
3. CountInYearRange(items []Item, from, to int, c *metrics.Counter) int
How many items fall inside a year range (inclusive).
Count every range check. Could you answer without looking at every item? Write your answer down — lessons 6 and 11 will change it.
4. Verify(items []Item) error
Checks that every item's Year and Rating are inside the bounds, and
returns an error naming the ID of the first one that is not.
Run it on your generated library. Then corrupt one line of library.jsonl by
hand and run it again.
5. AverageComparisons(items []Item, seed int64, trials int) float64
Picks trials titles at random from the library itself, searches for each,
and returns the mean comparison count.
With 1000 items and enough trials you should get about 500 — which is the average case we described. Check it.
Then do the same with titles that are not in the library. What do you get, and why is it different?