ProgrammingVery hardCodeforces-style · placement coding round
What is printed?
What is printed?
data = [3, 8, 15, 4, 22] res = [y for x in data if (y := x * 2) > 20] print(res, y)
- A. [30, 44] 6
- B. [30, 44] 44
- C. [44] 44
- D. [30, 44] 22
Correct answer: B. [30, 44] 44
The walrus target y is assigned on every pass, even when the condition fails, and it leaks into the enclosing scope. The last value assigned is 22 * 2 = 44.
More Python Programming questions
- A sorted array holds 10^6 integers and receives 10^5 queries asking how many values are at most x. Which approach is fastest in CPython?
- What is printed? python def depth(n): return 0 if n == 0 else 1 + depth(n - 1) try: print(depth(500)) print(depth(5000)) except…
- What is printed? python from collections import defaultdict d = defaultdict(list) d['a'].append(1) print('b' in d, d['b'], len(d), 'b' in d)
- What is printed? python m = [[1, 2, 3], [4, 5, 6]] rot = [list(r) for r in zip(m[::-1])] print(rot)
- What is printed? python def hist(h): st = [] best = 0 h = h + [0] for i, x in enumerate(h): start = i while st and st[-1][1] >= x: idx…
- What is printed? python def knap(items, cap): dp = [0] (cap + 1) for w, v in items: for c in range(cap, w - 1, -1): dp[c] = max(dp[c]…