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])
- A. [2, 1, 4] 2
- B. [0, 2, 1, 3] 2
- C. [0, 2, 1, 4] 2
- 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
- What is printed? python from itertools import combinations print(sum(1 for r in range(5) for c in combinations([1, 2, 3, 4], r) if sum(c)…
- What is printed? python print([i ^ (i >> 1) for i in range(4)])
- What is printed? python print(pow(6, -1, 11), pow(4, -1, 9))
- What is printed? python print(int.frombytes(b'x01x00', 'big'), int.frombytes(b'x01x00', 'little'))
- What is printed? python print((255).tobytes(2, 'big'), (1).tobytes(3, 'little'))
- What is printed? python print(sorted(range(10), key=lambda x: (x % 3, -x))[:4])