ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
from itertools import takewhile, dropwhile a = [1, 3, 4, 6, 7, 9] print(list(takewhile(lambda x: x % 2, a)), list(dropwhile(lambda x: x % 2, a)))
- A. [1, 3] [6, 7, 9]
- B. [1, 3, 5] [6, 7, 9]
- C. [1, 3] [4, 6, 7, 9]
- D. [1, 3, 7, 9] [4, 6]
Correct answer: C. [1, 3] [4, 6, 7, 9]
takewhile yields items while the predicate holds and stops at the first failure, which is 4. dropwhile skips the same odd prefix and then yields everything from 4 on, without rechecking later odd items.
More Python Programming questions
- What is printed? python def f(): try: return 'try' finally: print('finally', end=' ') print(f())
- What is printed? python def g(): try: return 1 finally: return 2 print(g())
- What is printed? python def bracketok(s): pairs = {')': '(', ']': '[', '}': '{'} st = [] for ch in s: if ch in '([{': st.append(ch) elif…
- What is printed? python import sys data = 'first line nsecondn' it = iter(data.splitlines()) print(repr(next(it)), repr(next(it).strip())…
- What is printed? python from collections import deque q = deque() for ch in 'abcd': q.append(ch) if len(q) > 2: q.popleft()…
- What is printed? python a = [1, 2, 3, 4, 5, 6, 7, 8] print(a[::3], a[2::-1], a[-3::2])