ProgrammingVery hardAmazon OA · Flipkart coding round
What is printed?
What is printed?
from functools import cmp_to_key
def largest(nums):
s = [str(x) for x in nums]
s.sort(key=cmp_to_key(lambda a, b: -1 if a + b > b + a else (1 if a + b < b + a else 0)))
return ''.join(s).lstrip('0') or '0'
print(largest([3, 30, 34, 5, 9]), largest([0, 0]))- A. 9543330 0
- B. 9534330 00
- C. 9534303 0
- D. 9534330 0
Correct answer: D. 9534330 0
A comparator that puts a before b when a+b is larger than b+a builds the largest concatenation. lstrip removes leading zeros, so [0, 0] becomes '0'.
More Python Programming questions
- What is printed? python from itertools import islice, count, takewhile sq = (i i for i in count(1)) print(list(islice(sq, 3))…
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def get(n): return [n] 2 a = get(3) a.append(99) b = get(3)…
- What is printed? python from collections import Counter a = Counter('aabbbc') b = Counter('abbd') print(a - b, (a + b)['b'], sorted((a &…
- What is printed? python print(pow(2, 100, 97), 2 100 % 97, (-2) 3 % 5, pow(-2, 3, 5))
- What is printed? python from itertools import permutations, combinations print(len(list(permutations('abcd', 2)))…
- What is printed? python def sqsorted(a): l, r = 0, len(a) - 1 out = [0] len(a) k = len(a) - 1 while l <= r: if abs(a[l]) > abs(a[r])…