ProgrammingVery hardAmazon OA · Flipkart coding round
What is printed?
What is printed?
import bisect a = [(1, 'x'), (3, 'y'), (3, 'z'), (7, 'w')] i = bisect.bisect_left(a, 3, key=lambda p: p[0]) j = bisect.bisect_right(a, 3, key=lambda p: p[0]) print(i, j, a[i:j])
- A. 1 2 [(3, 'y')]
- B. 0 3 [(1, 'x'), (3, 'y'), (3, 'z')]
- C. 1 4 [(3, 'y'), (3, 'z'), (7, 'w')]
- D. 1 3 [(3, 'y'), (3, 'z')]
Correct answer: D. 1 3 [(3, 'y'), (3, 'z')]
The key argument, available from Python 3.10, makes bisect compare only the first field. Left gives index 1 and right gives index 3, so the slice holds both 3s.
More Python Programming questions
- Which statement about counting pairs i < j with a[i] + a[j] == t is FALSE?
- What is printed? python a = [] for i in range(20): a.append(i) print(len(a), a[-1], a.pop(), len(a))
- What is printed? python print(int('0012'), int('ff', 16), int('0b11', 0), int('1234'))
- What is printed? python print(-7 // 2, -7 % 2, 7 // -2, 7 % -2, divmod(-7, 2))
- What is printed? python print(round(0.5), round(1.5), round(2.5), round(-0.5), round(2.675, 2))
- What is printed? python s = 'aaaa' print(s.count('aa'), s.replace('aa', 'b'), 'a,,b'.split(','), ' x y '.split(' '))