ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
print(sorted(['b10', 'b9', 'a10'], key=lambda s: (s[0], int(s[1:]))))
- A. ['a10', 'b10', 'b9']
- B. ['a10', 'b9', 'b11']
- C. ['a10', 'b9', 'b10']
- D. ['b9', 'b10', 'a10']
Correct answer: C. ['a10', 'b9', 'b10']
The key takes the letter first and then the number as an int, so b9 comes before b10 even though '9' is greater than '1' as text.
More Python Programming questions
- What is printed? python print(sorted('bAcB', key=str.upper), sorted('bAcB'))
- What is printed? python print(int('0' + '9' 3) + 1, int('9' 3 + '0') + 1)
- What is printed? python print(7 // 0.5, 7 % 0.5, -7 // 0.5)
- What is printed? python print(0.5 1074 == 0, 5e-324 > 0, 5e-324 / 2 == 0)
- What is printed? python print('abcabc'.replace('abc', 'x', 1), 'aaaa'.replace('aa', 'b'), 'aaa'.replace('', '-'))
- What is printed? python print('Mississippi'.lower().count('s'), 'Mississippi'.find('ss'), 'Mississippi'.rfind('ss'))