ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
words = ["banana", "kiwi", "apple", "fig"] print(sorted(words, key=len)) print(sorted(words, key=lambda w: w[-1]))
- A. ['fig', 'kiwi', 'apple', 'banana'] ['banana', 'apple', 'fig', 'kiwi']
- B. ['banana', 'apple', 'kiwi', 'fig'] ['fig', 'kiwi', 'apple', 'banana']
- C. ['fig', 'kiwi', 'apple', 'banana'] ['kiwi', 'banana', 'apple', 'fig']
- D. ['fig', 'apple', 'kiwi', 'banana'] ['banana', 'apple', 'fig', 'kiwi']
Correct answer: A. ['fig', 'kiwi', 'apple', 'banana'] ['banana', 'apple', 'fig', 'kiwi']
Sorting by len gives ascending lengths: fig(3), kiwi(4), apple(5), banana(6). Sorting by last character ('a','i','e','g') alphabetically orders them as 'a' banana, 'e' apple, 'g' fig, 'i' kiwi.
More Python Programming questions
- What is printed? python s = "Hello World" print(s.swapcase()) print(s.title()) print(" a b ".split())
- What is printed? python print("a-b--c".split("-")) print("a-b--c".split("-", 1))
- What is printed? python d = {"b": 2, "a": 1, "c": 3} print(sorted(d.items())) print(dict(sorted(d.items())))
- What is printed? python from functools import lrucache calls = [] @lrucache(maxsize=None) def f(n): calls.append(n) return n n f(2); f(3)…
- What is printed? python def deco(f): def wrapper(a, k): wrapper.calls += 1 return f(a, k) wrapper.calls = 0 return wrapper @deco def h()…
- What is printed? python def decoratorwithargs(prefix): def actual(f): def wrapper(a, k): return prefix + str(f(a, k)) return wrapper…