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.
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.
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.
Have the wrapper start the helper with best equal to −∞-\infty−∞, the value the invariant gives for an empty processed prefix.
4.
State the invariant: best is the largest reading in the processed prefix, or −∞-\infty−∞ when that prefix is empty.
5.
From that first call on, flag the current reading rrr when rrr exceeds best, and recur with max(best,r)\max(\text{best}, r)max(best,r) so the invariant holds for the longer prefix.
Unlock full course notes by signing up
Recovering the initial context
A repair invoice contains a fixed service charge plus parts costing 11, 8, and 17 dollars. There are no taxes, discounts, or other charges. The following helper adds the parts costs to an initial accumulator containing the service charge.
The call invoice([11, 8, 17], 0, service_charge) returns 64 dollars. What initial service charge was passed? Answer in dollars, to zero decimal places.
Preserving place value in an accumulator
A label scanner reads decimal digits from left to right. A recursive helper carries the whole number represented by the digits already processed. Each input element is an integer from zero through nine, and the list is nonempty.
UPDATE is a placeholder for the next accumulator value. Complete its description using operation names and an integer: first ________ the current accumulator by ten, then ________ the next digit. Before any digits have been processed, the accumulator starts at ________. This rule should correctly process a label such as [4, 0, 7].
Resuming without losing the processed prefix
A transit analyst summarizes delays of at least five minutes. The input delays is a finite list of nonnegative integer delays measured in minutes; it may be empty.
The standard initial call is summarize(delays, 0, 0, 0). For a resumed call, let k be an integer from zero through the list length, inclusive. Which statements are true? Select all that apply.
Spotting where a broken update loses context
A cycling route lists the distances between consecutive checkpoints as [4, 1, 6, 3] kilometres. Both functions below are meant to return each checkpoint's distance from the start, with the invariant that acc is the distance from the start to the point where the first remaining segment begins. Both are called with acc = 0.
Find the last value in the list returned by positions_a, in km; the last value in the list returned by positions_b, in km; the value of acc that positions_b receives in its last call with a non-empty list, in km; and the checkpoint number (counting from 1) at which the two output lists first differ. Give every answer as a whole number.