ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
it = iter([3, 1, 0, 5, 0])
out = []
for x in iter(lambda: next(it), 0):
out.append(x)
print(out, list(it))- A. [3, 1] [5, 0]
- B. [3, 1, 0] [5, 0]
- C. [3, 1] [0]
- D. [3, 1] [5]
Correct answer: A. [3, 1] [5, 0]
iter(callable, sentinel) stops when the callable returns the sentinel, and it consumes that 0. The list holds 3 and 1, and the iterator still has 5 and 0.
More Python Programming questions
- What is printed? python s = 'key=value=end' print(s.partition('='), s.rpartition('='), 'a b'.split(' '))
- What is printed? python def shift(s, k): return ''.join(chr((ord(c) - 97 + k) % 26 + 97) if c.islower() else c for c in s)…
- What is printed? python def closest3(a, t): a = sorted(a) best = None for i in range(len(a) - 2): l, r = i + 1, len(a) - 1 while l < r: s…
- What is printed? python print(0.0 == -0.0, -0.0, 1e16 + 1 == 1e16, 2 53 + 1 == 2 53)
- What is printed? python import bisect xs = [1, 4, 4, 9] i = bisect.bisectleft(xs, 4) xs.insert(i, 4) print(xs, bisect.bisectright(xs, 4)…
- What is printed? python from collections import deque d = deque(range(5)) d.rotate(-2) print(list(d), d[0], d[-1])