ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
from itertools import takewhile, dropwhile a = [1, 3, 5, 2, 7, 1] print(list(takewhile(lambda x: x % 2, a)), list(dropwhile(lambda x: x % 2, a)))
- A. [1, 3, 5, 7] [2, 7, 1]
- B. [1, 3] [5, 2, 7]
- C. [1, 3, 5] [2, 1]
- D. [1, 3, 5] [2, 7, 1]
Correct answer: D. [1, 3, 5] [2, 7, 1]
takewhile stops at the first even value, 2, and dropwhile skips the leading odd values and then keeps everything from 2 onward. The 7 is not included in the first result because takewhile has already stopped.
More Python Programming questions
- What is printed? python print(next(iter([]), 'none'), next(iter([7])))
- What is printed? python x = 3.14159 print(f'{x:.2f}|{42:>5}|{0.5:%}')
- What is printed? python print(0.1 + 0.2 + 0.3 == 0.6, 0.6 == 0.3 2, 0.3 2 == 0.6)
- What is printed? python print(sorted([10, 2, 33], key=str), sorted(['10', '2', '33'], key=int))
- What is printed? python print('%.2f' % 0.125, '%.2f' % 0.375, '%.1f' % 0.05, '%.0f' % 1.5)
- What is printed? python print(-17 % -5, 17 % -5, -17 % 5, -(17 % 5))