ProgrammingVery hardTCS NQT · Wipro pattern
What is printed?
What is printed?
import bisect a = [1, 3, 3, 5, 7] bisect.insort(a, 3) bisect.insort_right(a, 4, lo=0, hi=3) print(a, bisect.bisect(a, 3, 0, 2))
- A. [1, 3, 3, 3, 4, 5, 7] 2
- B. [1, 3, 3, 3, 5, 7, 4] 2
- C. [1, 3, 3, 4, 3, 5, 7] 2
- D. [1, 3, 3, 4, 3, 5, 7] 1
Correct answer: C. [1, 3, 3, 4, 3, 5, 7] 2
insort places 3 after the existing 3s, and insort_right with hi=3 searches only the first three items, so 4 lands at index 3 and the list is no longer sorted. The final bisect searches a[0:2] and returns 2.
More Python Programming questions
- What is printed? python def f(x): out = [] try: out.append('t') if x: raise ValueError except ValueError: out.append('e') else…
- What is printed? python def find(g, t): for i, row in enumerate(g): for j, v in enumerate(row): if v == t: break else: continue return (i…
- What is printed? python from functools import totalordering @totalordering class V: def init(self, n): self.n = n def eq(self, o): return…
- What is printed? python def make(): memo = {} def fib(n): if n in memo: return memo[n] memo[n] = n if n < 2 else fib(n - 1) + fib(n - 2)…
- What is printed? python words = 'the cat and the dog and the end'.split() cnt = {} for w in words: cnt[w] = cnt.get(w, 0) + 1 top =…
- What is printed? python def merge(iv): iv.sort() out = [iv[0]] for s, e in iv[1:]: if s <= out[-1][1]: out[-1][1] = max(out[-1][1], e)…