ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
from collections import Counter
c = Counter('aab')
c.subtract('ab')
print(c['a'], c['b'], sorted((+c).items()))
  1. A. 1 -1 [('a', 1), ('b', -1)]
  2. B. 0 -1 [('a', 0)]
  3. C. 1 0 [('a', 1)]
  4. D. 0 0 [('a', 1), ('b', 0)]

Correct answer: C. 1 0 [('a', 1)]

subtract lowers each count by one, leaving a at 1 and b at 0. Unary + drops entries whose count is zero or less, so only ('a', 1) remains.

More Python Programming questions

Exams that ask this topic