ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import bisect
a = sorted(['10', '9', '100', '20'])
print(a, bisect.bisect_left(a, '9'), [int(x) for x in a])
  1. A. ['9', '10', '20', '100'] 1 [9, 10, 20, 100]
  2. B. ['10', '100', '20', '9'] 3 [10, 100, 20, 9]
  3. C. ['10', '100', '20', '9'] 2 [10, 100, 20, 9]
  4. D. ['10', '9', '20', '100'] 3 [10, 9, 20, 100]

Correct answer: B. ['10', '100', '20', '9'] 3 [10, 100, 20, 9]

Strings sort character by character, so '10' < '100' < '20' < '9'. bisect_left('9') returns index 3, and the integers keep the string order.

More Python Programming questions

Exams that ask this topic