ProgrammingVery hardProduct company OA
What is printed?
What is printed?
calls = []
def chk(x):
calls.append(x)
return x > 2
r1 = any(chk(x) for x in [1, 3, 5])
r2 = all([chk(x) for x in [4, 0, 6]])
r3 = any(map(chk, [0, 7, 9]))
print(r1, r2, r3, calls)- A. True False True [1, 3, 5, 4, 0, 6, 0, 7, 9]
- B. True False True [1, 3, 4, 0, 0, 7]
- C. True False True [1, 3, 4, 0, 6, 0, 7]
- D. True True True [1, 3, 4, 0, 6, 0, 7]
Correct answer: C. True False True [1, 3, 4, 0, 6, 0, 7]
any and all short-circuit, but only lazy inputs benefit. The generator stops after chk(3) returns True (5 is never checked). The LIST comprehension calls chk on 4, 0 and 6 before all() even starts, then all returns False. map is lazy, so any stops after 7. Calls: 1, 3, 4, 0, 6, 0, 7.
More Python Programming questions
- What is printed? python a = [1, 2] b = [a] 3 a = 2 b[0].append(9) print(b[2], len(b))
- What is printed? python a = [1, 2] b = [a] 3 a = a 2 b[0].append(9) print(a, b[1])
- What is printed? python t = ([1],) try: t[0] += [2] except TypeError: pass try: t[0] = t[0] + [3] except TypeError: pass print(t)
- What is printed? python x = [1, 2, 3] y = x x[:] = [7] x = x + [8] print(y, x)
- What is printed? python m = [[0] 2 for in range(2)] n = m.copy() n[0][0] = 5 n[1] = [6, 6] print(m)
- What is printed? python d = {"k": []} e = dict(d) f = {k: v[:] for k, v in d.items()} d["k"].append(1) e["k"] += [2] f["k"] += [3]…