ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import itertools
print(list(itertools.accumulate([3, 1, 4, 1, 5], max))[-1], list(itertools.accumulate([3, 1, 4], initial=10)))
  1. A. 5 [13, 14, 18]
  2. B. 4 [10, 13, 14, 18]
  3. C. 5 [10, 3, 1, 4]
  4. D. 5 [10, 13, 14, 18]

Correct answer: D. 5 [10, 13, 14, 18]

accumulate with max gives a running maximum whose last value is 5. With initial=10 the first output is 10 and each later output adds the next item: 10, 13, 14, 18.

More Python Programming questions

Exams that ask this topic