ProgrammingVery hardCAT · MBA placement
What is printed?
What is printed?
from collections import Counter
c = Counter(a=1)
c.subtract({'a': 3, 'b': 1})
print(c, c['a'] + c['b'])- A. Counter({'a': -2, 'b': -1}) -3
- B. Counter({'a': -2}) -3
- C. Counter({'a': 2, 'c': 1}) 3
- D. Counter({'b': -1, 'a': -2}) -3
Correct answer: D. Counter({'b': -1, 'a': -2}) -3
subtract keeps negative counts and adds missing keys. a goes from 1 to -2 and b from 0 to -1, and the repr lists the largest count first, so b appears before a.
More Python Programming questions
- What is printed? python print(sorted([(2, 'b'), (1, 'z'), (3, 'a')], reverse=True))
- What is printed? python print(3 in {1, 2, 3} and 'x' in 'xyz', [] or () or None)
- What is printed? python def flat(x): for i in x: if isinstance(i, list): yield from flat(i) else: yield i print(list(flat([1, [2, [3, 4]]…
- What is printed? python print('42'.zfill(5), '-42'.zfill(5), 'ab'.center(6, ''), 'x'.ljust(3, '.') + '|')
- What is printed? python print(10 / 2, 10 // 2, 7 / 7, 2 3 + 4)
- What is printed? python print('abc'.index('c'), 'abcabc'.rfind('b'), 'abc'.find('d'), len('abc'[1:]))