ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
print(sorted(['b', 'B', 'a', 'A'], key=lambda s: s.swapcase()))
- A. ['a', 'b', 'A', 'B']
- B. ['A', 'a', 'B', 'b']
- C. ['a', 'A', 'b', 'a']
- D. ['b', 'a', 'B', 'A']
Correct answer: A. ['a', 'b', 'A', 'B']
The key is the swapcased letter, so 'A' becomes 'a' and so on. Sorting by these keys compares code points: the uppercase keys A and B (65, 66) come before a and b (97, 98), which gives a, b, A, B.
More Python Programming questions
- What is printed? python print(len(set('mississippi')), sorted(set('mississippi')))
- What is printed? python print(7 -1 == 1 / 7, pow(7, -1, 13) 7 % 13, (7 2) % 13, 2 3 2 % 10)
- What is printed? python x = [0] 3 x[0] += 1 y = [[0]] 2 y[0][0] += 1 print(x, y)
- What is printed? python print(int(float('1e16')) + 1, float(int(260) + 1) == float(260))
- What is printed? python print('a-bc d'.split('-'), 'a-bc d'.replace('-', ' ').split(''), 'a-b'.partition('-')[0])
- What is printed? python print(sum([1] 105) == 105, sum([0.5] 105) == 5e4, 1/3 + 1/3 + 1/3 == 1.0, 0.7 + 0.2 + 0.1 == 1.0)