An event-processing program computes an integer summary using a reduction, which combines successive values into one running result through a supplied callback.
def fold(combine, initial, values): result = initial for value in values: result = combine(result, value) return result def append_code(total, code): return 3 * total + code codes = [2, 4, 1, 5, 3] whole = fold(append_code, 0, codes) left = fold(append_code, 0, codes[:3]) right = fold(append_code, 0, codes[3:]) naive = append_code(left, right)
The first slice contains the first three codes, and the second contains the last two. A developer proposes replacing the sequential computation with separately computed chunk summaries.
Find whole and naive. Then find the constant multiplier w such that w * left + right reproduces the sequential result for every possible choice of integer codes with these same chunk lengths. Enter all three answers as exact, unitless integers.
Unlock full course notes by signing up
Return behavior for a later call
A game needs make_adjustment(delta) to return a reusable function that adds delta to a score supplied later. The final outer return statement is missing from this Python code:
Complete that statement by supplying only the function's identifier: return ________.
Dispatch jobs with tied sorting keys
A dispatch program orders jobs by urgency using the Python code below. Each pair contains a job label followed by its integer urgency level.
A key function returns the value that a sorting utility uses to order each item. Python’s sorted places smaller keys first, and jobs with equal keys retain their original input order.
Complete the table with each job’s returned key and its position in ordered_jobs, counting the first position as 1. Enter exact integers in every blank.
Analyze nested composition and call timing
A score-processing library constructs new functions using the Python code below. Select every true statement about the resulting functions and their evaluation.