ProgrammingVery hardProduct company OA
What is printed?
What is printed?
data = [5, 3, 8, 1]
out = []
while (n := len(data)) > 1:
first, *data = data
data[-1] += first
out.append((n, data[-1]))
print(out, data)- A. [(4, 6), (3, 9), (2, 17)] [17]
- B. [(4, 6), (3, 9), (2, 17)] [8, 17]
- C. [(3, 6), (2, 9), (1, 17)] [17]
- D. [(4, 6), (3, 11), (2, 19)] [19]
Correct answer: A. [(4, 6), (3, 9), (2, 17)] [17]
The walrus binds n to the length BEFORE the body shrinks the list. Star-unpacking builds a new list from the rest: first = 5, data = [3, 8, 1] → last becomes 6; first = 3, data = [8, 6] → 9; first = 8, data = [9] → 17. Then len is 1 and the loop stops. Output: [(4, 6), (3, 9), (2, 17)] [17]. The (3, …), (2, …), (1, …) option reads n after the unpacking.
More Python Programming questions
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def path(n): return [0] if n == 0 else path(n - 1) + [n] p…
- What is printed? python class Seq: def init(self, n): self.n = n; self.calls = 0 def getitem(self, i): self.calls += 1 if i >= self.n…
- What is printed? python import heapq tasks = [(2, "write"), (1, "test"), (2, "ship"), (1, "plan")] h = [] for i, (p, name) in…
- What is printed? python x = -13 print(x >> 2, x // 4, ~x, x & 0xF, bin(x & 0xFF).count("1"), (-x) >> 2)
- What is printed? python def chain(nums, divisors): g = (n for n in nums) for d in divisors: g = (n for n in g if n % d) return list(g)…
- What is printed? python class V: def init(self, n, log): self.n, self.log = n, log def add(self, o): self.log.append("add"); return…