ProgrammingHardTCS NQT · Wipro pattern
What is printed?
What is printed?
import heapq words = ['bb', 'a', 'cc', 'dd', 'e'] print(heapq.nlargest(2, words, key=len), heapq.nsmallest(2, words, key=len))
- A. ['bb', 'cc'] ['a', 'e']
- B. ['dd', 'cc'] ['a', 'e']
- C. ['bb', 'cc'] ['e', 'a']
- D. ['cc', 'dd'] ['a', 'e']
Correct answer: A. ['bb', 'cc'] ['a', 'e']
nlargest and nsmallest behave like a stable sort on the key, so among equal lengths the earlier word wins. For the smallest lengths, 'a' comes before 'e'.
More Python Programming questions
- What is printed? python data = [3, 8, 15, 4, 22] res = [y for x in data if (y := x 2) > 20] print(res, y)
- A sorted array holds 10^6 integers and receives 10^5 queries asking how many values are at most x. Which approach is fastest in CPython?
- What is printed? python def depth(n): return 0 if n == 0 else 1 + depth(n - 1) try: print(depth(500)) print(depth(5000)) except…
- What is printed? python from collections import defaultdict d = defaultdict(list) d['a'].append(1) print('b' in d, d['b'], len(d), 'b' in d)
- What is printed? python m = [[1, 2, 3], [4, 5, 6]] rot = [list(r) for r in zip(m[::-1])] print(rot)
- What is printed? python def hist(h): st = [] best = 0 h = h + [0] for i, x in enumerate(h): start = i while st and st[-1][1] >= x: idx…