ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
from collections import Counter
a = Counter("aabbbc")
b = Counter("abccd")
print(sorted((a - b).items()), sorted((a & b).items()), (a + b)["c"])- A. [('a', 1), ('b', 2), ('c', -1), ('d', -1)] [('a', 1), ('b', 1), ('c', 1)] 3
- B. [('a', 1), ('b', 2)] [('a', 1), ('b', 1), ('c', 1)] 3
- C. [('a', 1), ('b', 2)] [('a', 2), ('b', 3), ('c', 2), ('d', 1)] 3
- D. [('a', 1), ('b', 2), ('c', 0)] [('a', 1), ('b', 1), ('c', 1)] 3
Correct answer: B. [('a', 1), ('b', 2)] [('a', 1), ('b', 1), ('c', 1)] 3
Counter subtraction keeps only positive counts (c would be -1, d -1, both dropped). & takes the minimum count per key (keys absent in one side give 0 and vanish). + adds: c = 1 + 2 = 3.
More Python Programming questions
- What is printed? python a = {"x": 1, "y": 2} b = {"y": 3, "z": 4} c = a | b a |= {"w": 0} print(c, b | a)
- What is printed? python d = {i: i i for i in range(4)} try: for k in d: if k == 1: del d[k] except RuntimeError: print("RE", end=" ") for…
- What is printed? python d = {"a": 1} v = d.values() k = d.keys() d["b"] = 2 print(list(v), k - {"a"}, "b" in k)
- What is printed? python d = {} d.setdefault("k", []).append(1) d.setdefault("k", [9]).append(2) r = d.setdefault("n", None) print(d, r)
- What is printed? python s = {3, 1, 2} t = frozenset([1, 2]) print(t < s, s > t, s.isdisjoint({4}), {t: "ok"}[frozenset({2, 1})])
- What is printed? python a = [3, 1, 2] b = a.sort() or a c = sorted(a, reverse=True) a.reverse() print(b, c, b is a)