ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
import itertools
c = itertools.count()
data = ["a", "b", "c"]
pairs = list(zip(c, data))
print(pairs, next(c), list(itertools.compress("abcd", [1, 0, 1, 0])), list(itertools.takewhile(lambda x: x < 3, [1, 2, 5, 1])))- A. [(0, 'a'), (1, 'b'), (2, 'c')] 3 ['a', 'c'] [1, 2]
- B. [(0, 'a'), (1, 'b'), (2, 'c')] 4 ['a', 'c'] [1, 2, 1]
- C. [(0, 'a'), (1, 'b'), (2, 'c')] 4 ['a', 'c'] [1, 2]
- D. [(0, 'a'), (1, 'b'), (2, 'c')] 3 ['b', 'd'] [1, 2, 1]
Correct answer: C. [(0, 'a'), (1, 'b'), (2, 'c')] 4 ['a', 'c'] [1, 2]
zip pulls from count first: on the 4th round it takes 3 from count before finding data exhausted, so that value is lost and next(c) returns 4. takewhile stops for good at the first failing element (5).
More Python Programming questions
- What is printed? (Output lines are shown separated by " / ".) python def f(x): return x 2 g = f del f print(g(3), g.name) try: f(1) except…
- What is printed? python x = [1, 2, 3] it = iter(x) next(it) x.insert(0, 0) print(next(it), list(it))
- What is printed? python words = ["apple", "Banana", "cherry", "date"] bylen = {} for w in words: bylen.setdefault(len(w), []).append(w)…
- What is printed? python def outer(): results = [] def rec(n): if n == 0: results.append("x") return for in range(n): rec(n - 1) rec(3)…
- What is printed? python a = "abc" b = "".join(["a", "b", "c"]) print(a == b, a is b or True, sorted(set(a + b)), a.find("c") ==…
- What is printed? python temps = {"mon": 30, "tue": 25, "wed": 35} hot = [d for d, t in temps.items() if t > 28] coolest = min(temps…