ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
print(sorted(range(10), key=lambda x: (x % 3, -x))[:4])
- A. [9, 6, 3, 0]
- B. [0, 3, 6, 9]
- C. [9, 7, 6, 4]
- D. [0, 1, 2, 3]
Correct answer: A. [9, 6, 3, 0]
The key groups numbers by remainder mod 3, and inside each group -x puts the larger values first. Remainder 0 gives 9, 6, 3, 0, which are the first four.
More Python Programming questions
- What is printed? python print(''.join(sorted('banana', reverse=True)), len(set('abcABC'.lower())), 'ABC'.isupper())
- Which implementation of "count how many times each character appears in s" is the best choice in Python when len(s) = 2 10^5?
- What is printed? python from itertools import accumulate print(list(accumulate([1, 2, 3], initial=0)), next(iter(sorted({3, 1, 2}))))
- What is printed? python st = [] for t in '2 1 + 3 '.split(): if t.isdigit(): st.append(int(t)) else: b = st.pop() a = st.pop() st.append(a…
- What is printed? python a = [-2, 1, -3, 4, -1, 2, 1, -5, 4] best = cur = a[0] for x in a[1:]: cur = max(x, cur + x) best = max(best, cur)…
- What is printed? python print(sum(bin(i).count('1') for i in range(8)))