ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def key_fn(p):
return (-p[1], p[0])
people = [('bo', 3), ('al', 5), ('cy', 3)]
print([p[0] for p in sorted(people, key=key_fn)])- A. ['bo', 'cy', 'al']
- B. ['al', 'bo']
- C. ['al', 'bo', 'cy']
- D. ['al', 'cy', 'bo']
Correct answer: C. ['al', 'bo', 'cy']
The key sorts by the second field descending, so al with 5 comes first. Ties at 3 are broken by the name ascending, which gives bo before cy.
More Python Programming questions
- What is printed? python s = 'abcabc' print(s.find('c'), s.find('c', 3), s.rfind('c', 0, 4), s.count('c', 2))
- What is printed? python nums = [1, 2, 3, 4, 5] print([nums[i] for i in range(len(nums) - 1, -1, -2)], nums[::-2][0])
- What is printed? python def walk(s): total = 0 for i, ch in enumerate(s, start=1): if ch.isupper(): total += i else: total -= i return…
- What is printed? python print(sum(x for x in range(1, 11) if x & 1), sum(x for x in range(1, 11) if x & 2))
- What is printed? python x = 'AbC' print(x.swapcase(), x.lower() < x.upper(), sorted(x) == ['A', 'C', 'b'])
- What is printed? python print(max(range(5), key=lambda x: -abs(x - 2)), min(range(5), key=lambda x: abs(x - 2)))