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.


Python
def positions_a(dists, acc):
    if not dists:
        return []
    here = acc + dists[0]
    return [here] + positions_a(dists[1:], here)

def positions_b(dists, acc):
    if not dists:
        return []
    here = acc + dists[0]
    return [here] + positions_b(dists[1:], dists[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.

Your answers

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: