ProgrammingModerateTCS NQT · Accenture
What is printed?
What is printed?
from itertools import accumulate a = [3, 1, 4, 1, 5, 9, 2] print(list(accumulate(a, max)), list(accumulate(a, lambda x, y: x * y))[-1])
- A. [3, 3, 4, 4, 5, 9, 9] 1081
- B. [3, 1, 4, 1, 5, 9, 2] 1080
- C. [3, 4, 4, 5, 5, 9, 9] 1080
- D. [3, 3, 4, 4, 5, 9, 9] 1080
Correct answer: D. [3, 3, 4, 4, 5, 9, 9] 1080
accumulate with max keeps the running maximum, and with multiplication it keeps running products. The product of all seven values is 1080.
More Python Programming questions
- What is printed? python def comps(n, edges): p = list(range(n)) def find(x): while p[x] != x: p[x] = p[p[x]] x = p[x] return x cnt = n for…
- What is printed? python def spiral(m): out = [] while m: out += m.pop(0) m = [list(r) for r in zip(m)][::-1] return out print(spiral([[1…
- What is printed? python def climb(n, steps): dp = [0] (n + 1) dp[0] = 1 for i in range(1, n + 1): for s in steps: if i - s >= 0: dp[i] +=…
- What is printed? python from collections import deque def rot(g): R, C = len(g), len(g[0]) q = deque() fresh = 0 for r in range(R): for c…
- What is printed? python import heapq a, b, c = [1, 4, 7], [2, 5], [0, 3, 6] print(list(heapq.merge(a, b, c))[:5], next(heapq.merge(b, a)))
- What is printed? python import heapq def kth(m, k): h = [(m[0][0], 0, 0)] seen = {(0, 0)} for in range(k - 1): v, r, c = heapq.heappop(h)…