ProgrammingHardCodeforces-style · placement coding round
What is printed?
What is printed?
print(sorted([(2, 'b'), (1, 'z'), (3, 'a')], reverse=True))
- A. [(2, 'b'), (3, 'a'), (1, 'z')]
- B. [(3, 'a'), (2, 'b'), (1, 'z')]
- C. [(1, 'z'), (2, 'b'), (3, 'a')]
- D. [(3, 'a'), (1, 'z'), (2, 'b')]
Correct answer: B. [(3, 'a'), (2, 'b'), (1, 'z')]
reverse=True sorts the tuples from largest to smallest, comparing the number first and then the letter. So (3, 'a') comes first, followed by (2, 'b') and (1, 'z').
More Python Programming questions
- What is printed? python print(3 in {1, 2, 3} and 'x' in 'xyz', [] or () or None)
- What is printed? python def flat(x): for i in x: if isinstance(i, list): yield from flat(i) else: yield i print(list(flat([1, [2, [3, 4]]…
- What is printed? python print('42'.zfill(5), '-42'.zfill(5), 'ab'.center(6, ''), 'x'.ljust(3, '.') + '|')
- What is printed? python print(10 / 2, 10 // 2, 7 / 7, 2 3 + 4)
- What is printed? python print('abc'.index('c'), 'abcabc'.rfind('b'), 'abc'.find('d'), len('abc'[1:]))
- What is printed? python print(sorted('bca'), ''.join(sorted('bca', reverse=True)), sorted([3, 1, 2], reverse=True)[0], list(reversed('ab')))