ProgrammingModerateInfosys · Python
What is printed?
What is printed?
w = ['pear', 'fig', 'apple', 'kiwi', 'date'] print(sorted(w, key=len), sorted(w, key=len, reverse=True))
- A. ['fig', 'pear', 'kiwi', 'date', 'apple'] ['apple', 'pear', 'kiwi', 'date', 'fig']
- B. ['fig', 'kiwi', 'date', 'pear', 'apple'] ['apple', 'pear', 'kiwi', 'date', 'fig']
- C. ['fig', 'pear', 'kiwi', 'date', 'apple'] ['apple', 'date', 'kiwi', 'pear', 'fig']
- D. ['fig', 'apple', 'pear', 'kiwi', 'date'] ['apple', 'pear', 'kiwi', 'date', 'fig']
Correct answer: A. ['fig', 'pear', 'kiwi', 'date', 'apple'] ['apple', 'pear', 'kiwi', 'date', 'fig']
sorted is stable, and reverse=True also preserves the original order among equal keys (pear, kiwi, date stay in input order).
More Python Programming questions
- What is printed? python print(7 // 2, -7 // 2, 7 % -3, divmod(-7, 2), 2 -1)
- What is printed? python print({x % 3 for x in range(10)}, {x: x x for x in range(3)})
- What is printed? python def f(): try: return 'a' finally: print('f', end=' ') print(f())
- What is printed? python def g(): x = yield 1 print('got', x) y = yield x 2 print('got', y) it = g() print(next(it)) print(it.send(5)) try…
- What is printed? python def mk(): n = 0 def inc(): nonlocal n n += 1 return n return inc a, b = mk(), mk() print(a(), a(), b(), a())
- What is printed? python class C: v = [] n = 0 def add(self, x): self.v.append(x) self.n += 1 a, b = C(), C() a.add(1); b.add(2); b.add(3)…