ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
from itertools import groupby s = 'aabbaa' print([(k, len(list(g))) for k, g in groupby(s)])
- A. [('a', 4), ('b', 2)]
- B. [('a', 2), ('b', 2)]
- C. [('b', 2), ('a', 4)]
- D. [('a', 2), ('b', 2), ('a', 2)]
Correct answer: D. [('a', 2), ('b', 2), ('a', 2)]
groupby merges only consecutive equal keys, so the two runs of a are reported separately. People who expect a dict or Counter result often get this wrong.
More Python Programming questions
- What is printed? python print(sorted(['b', 'A', 'a', 'B'], key=str.lower))
- What is printed? python nums = map(int, '1 2 3'.split()) a = list(nums) b = list(nums) print(a, b)
- What is printed? python print(1020 + 1 - 1020, float(1020 + 1) - float(1020))
- What is printed? python data = [('b', 2), ('a', 2), ('c', 1), ('a', 1)] print(sorted(sorted(data, key=lambda t: t[0]), key=lambda t: t[1]…
- What is printed? python print(int('0x1F', 16), int('1F', 16), int('0b101', 0), int(' 42 '), int('42'))
- What is printed? python print(int('٣') + 1)