Syllabus Lesson 65 of 141 · Data models that bend
Data models that bend

Flat is fine

Why it matters After four models that needed more structure, it is tempting to build a nested, indexed model for everything. For a small table that is read in new ways every week, a plain list of rows bends better than any structure.

A facilities team logs temperature readings as rows of (day, site, value), a few hundred a season. They keep asking new questions of it: per site this week, by date next week, "anything above 30?" after that. Every question is a filter over the same rows.

Your turn

Write load(rows) (returns your model), average(model, site) (mean value, or None if the site has no readings), between(model, start, end) (rows with start <= day < end) and above(model, level) (rows with value > level). Rows are (day, site, value) tuples; lists of rows keep the original order. Rule: your whole code may have at most 130 syntax nodes (code_size). There are a few hundred rows.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.