ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
print(sorted(['b2', 'a10', 'a9', 'b1'], key=lambda s: (s[0], int(s[1:]))), sorted(['b2', 'a10', 'a9', 'b1']))
- A. ['a9', 'a10', 'b1', 'b2'] ['a10', 'a9', 'b1', 'b2']
- B. ['a10', 'a9', 'b1', 'b2'] ['a9', 'a10', 'b1', 'b2']
- C. ['a9', 'a10', 'b1', 'b2'] ['a9', 'a10', 'b1', 'b2']
- D. ['a9', 'b1', 'a10', 'b2'] ['a10', 'a9', 'b1', 'b2']
Correct answer: A. ['a9', 'a10', 'b1', 'b2'] ['a10', 'a9', 'b1', 'b2']
The first key compares the letter and then the number, so a9 comes before a10. The plain sort compares strings character by character, so '10' sorts before '9' inside the a group.
More Python Programming questions
- What is printed? python from math import isclose print(isclose(0, 1e-10), isclose(0, 1e-10, abstol=1e-9), isclose(1e9, 1e9 + 1)…
- What is printed? python x = 0b10110110 print(format(x, '08b')[::-1], x.bitcount(), x & (x - 1), (x & -x).bitlength())
- What is printed? python print(f'{1:>{3}}', '{0[1]}-{1!r}'.format('ab', 'cd'), '{0!r:>6}'.format('x'), '{:.1%}'.format(0.256))
- What is printed? python x = 232 + 5 print((x 0x9E3779B1) & 0xFFFFFFFF, (5 0x9E3779B1) & 0xFFFFFFFF)
- What is printed? python import math print(math.factorial(20) // 106 % 1000, len(str(math.factorial(30)))…
- What is printed? python print(float('0.1') == 0.1, float(' 0.1 ') == 0.1, 0.1 == float(str(0.1)), 1e23 == 1023)