ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
print(sorted(['10', '9', '100', '8'], key=len), sorted(['10', '9', '100', '8'], key=int))
- A. ['9', '8', '10', '100'] ['8', '9', '10', '100']
- B. ['8', '9', '10', '100'] ['9', '8', '10', '100']
- C. ['9', '8', '10', '100'] ['10', '100', '8', '9']
- D. ['9', '10', '8', '100'] ['8', '9', '10', '100']
Correct answer: A. ['9', '8', '10', '100'] ['8', '9', '10', '100']
Sorting by len is stable, so the one-character strings keep their input order: 9 then 8. Sorting by int orders the strings numerically, giving 8, 9, 10, 100.
More Python Programming questions
- What is printed? python from math import isqrt print(isqrt(1030), isqrt(99), isqrt(1030 - 1))
- What is printed? python print(sum(1 for in range(105) if '7' in str()), len(str(sum(1 for in range(105) if '7' in str()))))
- What is printed? python print('%s' % (1,), '%s' % ((1,),), '%r' % ((1, 2),))
- What is printed? python print(1 if True else 0 or 2, not 0 or 3, (not 0) or 3, 0 or '' or None)
- What is printed? python print(max(map(str, [9, 10, 2])), max([9, 10, 2]), min(map(int, ['10', '9'])))
- What is printed? python x = 0.1 3 y = 0.3 print(x == y, abs(x - y) < 1e-15, round(x, 15) == round(y, 15), x - y)