Sequencing the design of a record-tracking accumulator

A function records(readings) must return a list of True/False flags, one per daily temperature reading, where a flag is True when that reading is higher than every earlier reading in the list. Put the following design steps in the order in which each one depends on the step before it.

  1. 1.

    At the empty list, return an empty list of flags: because every update kept the invariant, best now summarises the whole input, but no readings remain to flag.

  2. 2.

    Notice that deciding whether a reading is a record needs the earlier readings, which a plain recursive call on the rest of the list cannot see.

  3. 3.

    Have the wrapper start the helper with best equal to −∞-\infty, the value the invariant gives for an empty processed prefix.

  4. 4.

    State the invariant: best is the largest reading in the processed prefix, or −∞-\infty when that prefix is empty.

  5. 5.

    From that first call on, flag the current reading rr when rr exceeds best, and recur with max⁡(best,r)\max(\text{best}, r) so the invariant holds for the longer prefix.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Context-Preserving Accumulators Questions: