ProgrammingHardCAT · MBA placement
What is printed?
What is printed?
from itertools import islice, count, takewhile sq = (i * i for i in count(1)) print(list(islice(sq, 3)), list(takewhile(lambda x: x < 30, (i * i for i in count(1)))), next(sq))
- A. [1, 4, 9] [1, 4, 9, 16] 16
- B. [1, 4] [1, 4, 9, 16, 25] 9
- C. [1, 4, 9] [1, 4, 9, 16, 25] 9
- D. [1, 4, 9] [1, 4, 9, 16, 25] 16
Correct answer: D. [1, 4, 9] [1, 4, 9, 16, 25] 16
islice takes three values from the shared generator, so next(sq) continues at 16. takewhile uses a fresh generator and stops at the first square that is not below 30.
More Python Programming questions
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def get(n): return [n] 2 a = get(3) a.append(99) b = get(3)…
- What is printed? python from collections import Counter a = Counter('aabbbc') b = Counter('abbd') print(a - b, (a + b)['b'], sorted((a &…
- What is printed? python print(pow(2, 100, 97), 2 100 % 97, (-2) 3 % 5, pow(-2, 3, 5))
- What is printed? python from itertools import permutations, combinations print(len(list(permutations('abcd', 2)))…
- What is printed? python def sqsorted(a): l, r = 0, len(a) - 1 out = [0] len(a) k = len(a) - 1 while l <= r: if abs(a[l]) > abs(a[r])…
- What is printed? python def minpath(g): dp = [0] len(g[0]) for i, row in enumerate(g): for j, v in enumerate(row): if i == 0 and j == 0…