ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
import bisect
board = []
for name, score in [('a', 5), ('b', 9), ('c', 5)]:
bisect.insort(board, (-score, name))
print([n for _, n in board])- A. ['b', 'c', 'a']
- B. ['b', 'a', 'c']
- C. ['a', 'b', 'c']
- D. ['c', 'b', 'a']
Correct answer: B. ['b', 'a', 'c']
Storing (-score, name) keeps the list sorted by highest score first and then by name. The 9 comes first, then the two 5s in alphabetical order.
More Python Programming questions
- What is printed? python print(sum([1, 2], 0.5))
- What is printed? python import heapq def topo(n, edges): indeg = [0] n adj = [[] for in range(n)] for u, v in edges: adj[u].append(v)…
- Which operation is O(1) amortised in CPython and removes the first item, while list.pop(0) is O(n)?
- What is printed? python print(sorted('bca', reverse=True)[0], 'bca'.index('c'))
- What is printed? python from collections import defaultdict d = defaultdict(int) d['x'] += 2 d['y'] print(dict(d))
- What is printed? python print(list(map(pow, [2, 3], [3, 2])))