ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
print(sorted(['x10', 'x9', 'x1'], key=lambda s: int(s[1:])))
- A. ['x1', 'x10', 'x9']
- B. ['x1', 'x9', 'x10']
- C. ['x10', 'x1', 'x9']
- D. ['x9', 'x1', 'x10']
Correct answer: B. ['x1', 'x9', 'x10']
The key takes the number after the x, so the values are 10, 9 and 1. Sorting those numbers numerically gives x1, x9 and x10.
More Python Programming questions
- What is printed? python print(0.1 3 == 0.3, 0.1 3 == 3 0.1, round(0.1 3, 1) == 0.3, 1 - 0.9 == 0.1)
- What is printed? python print('%s' % [1, 'a'], '%r' % 'a', '%5.2s|' % 'hello', '%c' % 65)
- What is printed? python print(hash(1.5) == hash(3/2), hash(True) == hash(1), {0: 'x', False: 'y'}, len({1: 'a', True: 'b', 1.0: 'c'}))
- What is printed? python print(min(3, 2.0, True), max(1, 1.0), max(1.0, 1))
- What is printed? python print(253 == float(253), 253 + 1 == float(253 + 1), 253 + 2 == float(253 + 2))
- What is printed? python s = 'atbnc' print(len(s), s.split(), s.split('n'), repr(s.split()[0]))