ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
from itertools import groupby
print([(k, len(list(g))) for k, g in groupby('aabbaa')])
  1. A. [('a', 4), ('b', 2)]
  2. B. [('a', 2), ('b', 2)]
  3. C. [('a', 2), ('b', 2), ('a', 3)]
  4. 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

Exams that ask this topic