ProgrammingHardTCS NQT · Python
What is printed?
What is printed?
pts = [(2, 1), (1, 5), (2, 0), (1, 2)] print(sorted(pts, key=lambda p: (-p[0], p[1])), max(pts, key=lambda p: p[1]))
- A. [(2, 0), (2, 1), (1, 2), (1, 5)] (2, 1)
- B. [(2, 1), (2, 0), (1, 5), (1, 2)] (1, 5)
- C. [(1, 2), (1, 5), (2, 0), (2, 1)] (1, 5)
- D. [(2, 0), (2, 1), (1, 2), (1, 5)] (1, 5)
Correct answer: D. [(2, 0), (2, 1), (1, 2), (1, 5)] (1, 5)
The key (-first, second) sorts first by descending first element, then ascending second; max picks the pair with the largest second element.
More Python Programming questions
- What is printed? python import copy a = {'k': [1, 2]} b = a.copy() c = copy.deepcopy(a) a['k'].append(3) a['j'] = 0 print(b, c)
- What is printed? python m = map(lambda x: x 2, [1, 2, 3]) print(list(m), list(m), sum(map(int, '123')))
- What is printed? python class P: def init(self, v): self.v = v def eq(self, o): return self.v == o.v try: s = {P(1), P(1)} print(len(s))…
- What is printed? python fs = [lambda x, i=i: x i for i in range(3)] gs = [lambda x: x i for i in range(3)] print([f(2) for f in fs], [g(2)…
- What is printed? python n = 1 xs = [1, 2, 3] g = (x for x in xs if x > n) xs = [10, 20] n = 2 print(list(g))
- What is printed? python print(f"{255:#x} {255:08b} {3.14159:.2f} {'ab':>4}|{12345.678:,.1f}")