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.


Python
def build_number(digits, i, acc):
    if i == len(digits):
        return acc
    return build_number(digits, i + 1, UPDATE)


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].

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: