ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
def pipeline():
nums = (n for n in range(10))
sq = (n * n for n in nums if n % 2)
big = (s for s in sq if s > 10)
return next(big), list(nums)
print(pipeline())- A. (25, [])
- B. (25, [5, 6, 7, 8, 9])
- C. (9, [4, 5, 6, 7, 8, 9])
- D. (25, [6, 7, 8, 9])
Correct answer: D. (25, [6, 7, 8, 9])
Chained generators pull lazily: to get the first value above 10, sq consumes nums up to 5 (squares 1, 9 rejected, 25 accepted). nums is left positioned after 5, so list(nums) gives 6..9.
More Python Programming questions
- What is printed? python def g(): print("a", end=" ") x = yield print("b", x, end=" ") yield x 2 it = g() print(it.send(None), end=" ")…
- What is printed? python def g(): try: yield 1 except GeneratorExit: print("exit", end=" ") yield 2 it = g() next(it) try: it.close()…
- What is printed? (Output lines are shown separated by " / ".) python it = iter([1, 2, 3, 4, 5, 6]) print([(a, b) for a, b in zip(it, it)]…
- What is printed? python s = {1, 2, 3} t = {3, 4} print(s | t, s & t, s - t, s ^ t, s >= {1, 2})
- What is printed? python a = {1, 2} b = a a |= {3} c = frozenset(a) a = a | {4} print(b, c, a, type(c | {9}).name)
- What is printed? python s = set() s.add((1, 2)) s.add((2, 1)) try: s.add([1]) except TypeError: s.add(frozenset([1]))…