ProgrammingHardTCS NQT · Wipro pattern
What is printed?
What is printed?
words = ['bb', 'a', 'ccc', 'aa'] print(sorted(words, key=lambda w: (-len(w), w)))
- A. ['ccc', 'bb', 'aa', 'a']
- B. ['a', 'aa', 'bb', 'ccc']
- C. ['ccc', 'aa', 'bb', 'a']
- D. ['ccc', 'bb', 'a', 'aa']
Correct answer: C. ['ccc', 'aa', 'bb', 'a']
The key sorts by length in descending order and then alphabetically. ccc is the longest, then aa and bb in alphabetical order, and the single letter a comes last.
More Python Programming questions
- What is printed? python pairs = [(1, 'b'), (1, 'a'), (0, 'c')] print(sorted(pairs, key=lambda p: p[0]), sorted(pairs))
- What is printed? python print(int('0b11', 0) + int('0o11', 0) + int('0x11', 0), int('11', 16), int('-0b11', 0))
- What is printed? python from math import inf print(inf - 1 == inf, 2 inf, -inf < -10 400, inf > 10 400)
- What is printed? python print(sum(1 for c in 'a1 b2' if c.isalpha()), sum(1 for c in 'a1 b2' if c.isdigit()), sum(c.isspace() for c in 'a1…
- What is printed? python print(round(2.675, 2), round(2.665, 2), round(1.0049999999999999, 3), round(-2.5, 0))
- What is printed? python print(max('apple', 'banana', key=len), max(['x', 'yy', 'zz'], key=len), min([3, 1, 1.0], key=abs), max([1, 1.0]…