ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import bisect
a = [3, 1, 2, 5, 4]
seen = []
inv = 0
for x in a:
inv += len(seen) - bisect.bisect_right(seen, x)
bisect.insort(seen, x)
print(inv)- A. 2
- B. 5
- C. 3
- D. 4
Correct answer: C. 3
For each element, count how many earlier elements are greater than it. The inversions in [3, 1, 2, 5, 4] are (3,1), (3,2) and (5,4), so the total is 3.
More Python Programming questions
- What is printed? python print(float('nan') == float('nan'), float('inf') - float('inf') != float('inf') - float('inf'))
- What is printed? python try: print([1, 2] < (1, 2)) except TypeError as e: print(e)
- What is printed? python print(sum(i for i in range(1, 101) if i % 3 == 0 or i % 5 == 0))
- A solution must answer q = 2 10^5 queries of the form 'how many values in array A are at most x', with A of length n = 2 10^5 that never…
- A program builds a long output string with s = s + str(i) + ' ' for each of n = 2 10^5 values i. Which change is the standard fix for…
- What is printed? python rows = [('bob', 90), ('amy', 90), ('cat', 75)] rows.sort(key=lambda r: (-r[1], r[0])) print([r[0] for r in rows])