ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
nums = [13, 22, 5, 31, 8, 3] print(sorted(nums, key=lambda x: x % 10), sorted(nums, key=lambda x: x % 10, reverse=True))
- A. [1, 22, 13, 3, 5, 8] [8, 5, 13, 3, 22, 31]
- B. [31, 22, 13, 3, 5, 8] [8, 5, 13, 3, 22, 31]
- C. [31, 22, 3, 13, 5, 8] [8, 5, 13, 3, 22, 31]
- D. [31, 22, 13, 3, 5, 8] [8, 5, 3, 13, 22, 31]
Correct answer: B. [31, 22, 13, 3, 5, 8] [8, 5, 13, 3, 22, 31]
The keys are 3, 2, 5, 1, 8, 3. 13 and 3 tie on key 3. Both sorts keep equal keys in their original order, including reverse=True, so 13 stays ahead of 3 in both results.
More Python Programming questions
- What is printed? python def gen(): yield 1 return 5 yield 2 it = gen() print(list(it), next(it, 'done'))
- What is printed? python from itertools import accumulate print(list(accumulate([2, 3, 4], lambda a, b: a b)), list(accumulate([1, -1, 2])))
- What is printed? python a = [[1, 2], [3, 4]] b = [row[:] for row in a] c = a[:] a[0][0] = 9 c[1][1] = 7 print(b, c)
- What is printed? python print(len(str(2 64)), (2 64).bitlength(), 2 64 % 9, pow(2, 64, 9))
- Which technique gives the lowest total cost for answering q range-sum queries on a fixed Python list of length n, where n and q are both 2…
- What is printed? python x = 7 print(x // 2 if x % 2 else x / 2, x > 3 > 2, not x > 3 == 4)