ProgrammingVery hardProduct company OA
What is printed?
What is printed?
rows = [("x", 2), ("y", 1), ("z", 2), ("w", 1), ("v", 3)]
a = sorted(rows, key=lambda r: r[1], reverse=True)
b = sorted(rows, key=lambda r: -r[1])
c = sorted(sorted(rows), key=lambda r: r[1])
print("".join(r[0] for r in a), "".join(r[0] for r in b), "".join(r[0] for r in c), a == b)- A. vzxwy vxzyw wyxzv False
- B. vxzyw vxzyw ywxzv True
- C. vxzyw vxzyw wyxzv True
- D. vzxwy vxzyw ywxzv False
Correct answer: C. vxzyw vxzyw wyxzv True
Python's sort is stable, and reverse=True is defined to keep that stability (equal keys stay in original order; it is not 'sort then reverse'). So a and b are identical: v, then x before z, then y before w → 'vxzyw'. For c the inner sort orders by name (v, w, x, y, z) and the stable outer sort by number keeps that order inside ties: w, y, x, z, v. 'vzxwy' is what reversing an ascending sort would give.
More Python Programming questions
- What is printed? python class P: def init(self, x, y): self.x, self.y = x, y def eq(self, o): return self.x == o.x def hash(self): return…
- What is printed? python it = iter(range(10)) a = list(zip(it, "ab")) b = next(it) c = [x for x in it if next(it, 0) % 3] print(a, b, c)
- What is printed? python def tag(t): print("make", t, end=" ") def deco(fn): print("wrap", t, end=" ") def inner(a): return f"<{t}>" +…
- What is printed? python x = 1 class K: x = 2 a = [x for in range(2)] b = [x i for i in range(x + 1)] def m(self): return x print(K.a, K.b…
- What is printed? python data = [5, 3, 8, 1] out = [] while (n := len(data)) > 1: first, data = data data[-1] += first out.append((n…
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def path(n): return [0] if n == 0 else path(n - 1) + [n] p…