ProgrammingHardTCS NQT · Wipro pattern
What is printed?
What is printed?
print(sorted(['pear', 'Fig', 'apple', 'banana', 'kiwi'], key=lambda s: (len(s), s.lower())))
- A. ['Fig', 'pear', 'kiwi', 'apple', 'banana']
- B. ['kiwi', 'Fig', 'pear', 'apple', 'banana']
- C. ['Fig', 'kiwi', 'apple', 'pear', 'banana']
- D. ['Fig', 'kiwi', 'pear', 'apple', 'banana']
Correct answer: D. ['Fig', 'kiwi', 'pear', 'apple', 'banana']
The key is a tuple of length and then lowercase text, so Fig (3 letters) comes first. Among the four-letter words, kiwi sorts before pear, and the five- and six-letter words follow.
More Python Programming questions
- What is printed? python print(sorted([('b', 1), ('a', 1), ('c', 2)], key=lambda t: t[1], reverse=True))
- What is printed? python print(max(['abc', 'x', 'yz'], key=len), min(['b', 'B', 'a']))
- What is printed? python from decimal import Decimal from fractions import Fraction print(0.1 + 0.2 == 0.3, Decimal('0.1') + Decimal('0.2')…
- What is printed? python print(1e22, 1e21, 1e16, 1e15, 1e-5, 0.0001)
- What is printed? python print(int(1e16 + 1), 253 + 1 == float(253 + 1), float(253 + 1) == 253)
- What is printed? python print(pow(10, 18, 7), (-3) 3 % 7, pow(-3, 3, -7), -3 3 % 7)