ProgrammingHardTCS NQT · Wipro pattern
What is printed?
What is printed?
x = [3, 1, 2] print(sorted(x, reverse=True)[:2], x[::-1][1:], list(reversed(sorted(x))))
- A. [3, 2] [1, 3] [3, 2, 1]
- B. [3, 1] [1, 3] [3, 2, 1]
- C. [3, 2] [2, 1] [1, 2, 3]
- D. [3, 2] [1, 3] [1, 2, 3]
Correct answer: A. [3, 2] [1, 3] [3, 2, 1]
sorted with reverse gives 3, 2, 1, so the first two are 3 and 2. x[::-1] is 2, 1, 3 and dropping the first gives 1, 3. reversed(sorted(x)) walks the ascending list backwards.
More Python Programming questions
- What is printed? python print('%08.2f' % -1.5, '%+08d' % 42, '%-+5d|' % 7, '%x' % -255)
- What is printed? python print(2 10 == 1024, 2 10 == 1 << 10, 10 2 3, (10 2) 3)
- What is printed? python from itertools import accumulate print(list(accumulate([1, 2, 3], lambda a, b: a b)), list(accumulate('ab'…
- What is printed? python print(sorted({'b': 1, 'a': 1}.items()), list({3: 'x', 1: 'y'}), dict(zip('ab', [1, 2])), dict.fromkeys('ab', 0))
- What is printed? python print(len({1, 1.0, True, '1'}), len({'1', 1}), {1, 2} == {2.0, 1.0}, {0} == {False})
- What is printed? python print(float('1' + '0' 308) == 1e308, 10 308 > 1e308, 10 308 < 1e308)