ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
from operator import itemgetter
rows = [('b', 2, 'x'), ('a', 2, 'y'), ('c', 1, 'z')]
print([r[0] for r in sorted(rows, key=itemgetter(1, 0))])- A. ['a', 'b', 'c']
- B. ['b', 'a', 'c']
- C. ['c', 'a', 'b']
- D. ['c', 'b', 'a']
Correct answer: C. ['c', 'a', 'b']
itemgetter(1, 0) builds the key (second field, first field). The sorted keys are (1, 'c'), (2, 'a'), (2, 'b'), giving c, a, b.
More Python Programming questions
- What is printed? python import bisect print(bisect.bisect([1, 2, 2, 5], 2))
- What is printed? python try: sorted([3, 'a']) except TypeError as e: print(e)
- A Python solution reads n = 10^6 numbers with input() in a loop, calling int(input()) for each line. Which change is the most effective…
- What is printed? python import math print(math.gcd(12, 18, 27), math.lcm(4, 6), math.gcd())
- What is printed? python print(divmod(-(10 20), 7))
- What is printed? python import heapq h = [4, 1, 3] heapq.heapify(h) print(h[0], sorted(h))