ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
from itertools import accumulate
a = [2, -1, 3]
P = list(accumulate(a, initial=0))
print(P, P[3] - P[1])
  1. A. [2, 1, 4] 2
  2. B. [0, 2, 1, 3] 2
  3. C. [0, 2, 1, 4] 2
  4. D. [0, 2, 1, 4] 3

Correct answer: C. [0, 2, 1, 4] 2

Prefix sums with initial=0 give [0, 2, 1, 4]. The sum of a[1:3] is P[3] - P[1] = 4 - 2 = 2, which is -1 + 3.

More Python Programming questions

Exams that ask this topic