ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
from functools import cmp_to_key
w = ['bb', 'a', 'ccc', 'ab', 'c']
def c(x, y):
if len(x) != len(y):
return len(x) - len(y)
return (y > x) - (y < x)
print(sorted(w, key=cmp_to_key(c)))- A. ['c', 'a', 'bb', 'ab', 'ccc']
- B. ['a', 'c', 'ab', 'bb', 'ccc']
- C. ['a', 'c', 'bb', 'ab', 'ccc']
- D. ['c', 'a', 'ab', 'bb', 'ccc']
Correct answer: A. ['c', 'a', 'bb', 'ab', 'ccc']
Length is compared first, ascending. For equal lengths the comparator returns positive when y > x, which puts the larger string first, so the order is descending lexicographic within each length.
More Python Programming questions
- What is printed? python from collections import deque g = ["..#", ".#.", "..."] d = [[-1] 3 for in range(3)] q = deque([(0, 0)]) d[0][0] =…
- What is printed? python import bisect a = sorted([5, 1, 9, 3, 7]) print(bisect.bisectright(a, 7) - bisect.bisectleft(a, 4))
- What is printed? python from collections import defaultdict d = defaultdict(list) d['a'] print('a' in d, len(d), d.get('b'), 'b' in d)
- What is printed? python from functools import cache @cache def paths(r, c): return 1 if r == 0 or c == 0 else paths(r - 1, c) + paths(r, c…
- What is printed? python print(sum(map(int, str(2 100))))
- What is printed? python print(bin(12345).count('1'), len(bin(12345)) - 2)