ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def sort_count(a):
if len(a) <= 1:
return a, 0
m = len(a) // 2
l, x = sort_count(a[:m])
r, y = sort_count(a[m:])
out, i, j, c = [], 0, 0, x + y
while i < len(l) and j < len(r):
if l[i] <= r[j]:
out.append(l[i])
i += 1
else:
out.append(r[j])
j += 1
c += len(l) - i
out += l[i:] + r[j:]
return out, c
print(sort_count([2, 4, 1, 3, 5])[1], sort_count([5, 4, 3])[0])- A. 4 [3, 4, 5]
- B. 3 [5, 4, 3]
- C. 2 [3, 4, 5]
- D. 3 [3, 4, 5]
Correct answer: D. 3 [3, 4, 5]
Merge sort counts inversions while merging: each time an item from the right half moves first, every item still left in the left half forms an inversion with it. The first list has 3 inversions and the second sorts to 3, 4, 5.
More Python Programming questions
- What is printed? python print([10, 20][True], [10, 20][False])
- What is printed? python print(repr('ab' -1), [1] 0, 'a' 3 == 'aaa')
- What is printed? python print(all([]), any([]), all([0, 1]), any([0, '']))
- What is printed? python print([3, 1].sort() or 'x', sorted([3, 1]) == [1, 3])
- What is printed? python try: try: 1 / 0 except ZeroDivisionError: raise ValueError('bad') except ValueError as e…
- What is printed? python print(sorted([(1, 2), (1,), (0, 9, 9)]))