ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
from itertools import groupby
print([(k, len(list(g))) for k, g in groupby('aabbaa')])- A. [('a', 4), ('b', 2)]
- B. [('a', 2), ('b', 2)]
- C. [('a', 2), ('b', 2), ('a', 3)]
- D. [('a', 2), ('b', 2), ('a', 2)]
Correct answer: D. [('a', 2), ('b', 2), ('a', 2)]
groupby only groups consecutive equal items, so the two runs of a are reported separately. The runs are aa, bb and aa, each of length two.
More Python Programming questions
- What is printed? python print('a,b'.split(',', -1), 'a,b,c'.split(',', 0), 'a b c'.split(maxsplit=1))
- What is printed? python print(bytes.fromhex('de ad'), (255).tobytes(2), bytes([1, 2]).hex(' '))
- What is printed? python print(round(1.5, 0), round(-2.5), round(2.5, 0) == 2, round(0.5, 0) == 0)
- What is printed? python from math import floor, ceil, trunc print(floor(-0.5), ceil(-0.5), trunc(-0.5), floor(2))
- What is printed? python print(sorted([3, 1, 2], key=lambda x: -x)[0], sorted('bca', key=lambda c: c > 'a'))
- What is printed? python print(int('10', 36) == 36, int('Z', 36), int('z', 36) + 1, int('10', 2) == 2)