ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
d = {}
for w in "to be or not to be".split():
d[w] = d.get(w, 0) + 1
best = max(d, key=d.get)
print(best, sorted(d.items(), key=lambda kv: (-kv[1], kv[0]))[:3])- A. be [('be', 2), ('to', 2), ('not', 1)]
- B. to [('be', 2), ('to', 2), ('not', 1)]
- C. to [('to', 2), ('be', 2), ('or', 1)]
- D. to [('be', 2), ('to', 2), ('or', 1)]
Correct answer: B. to [('be', 2), ('to', 2), ('not', 1)]
max over a dict iterates its keys in insertion order and returns the first with the maximal count, 'to'. The sort key orders by count descending and then alphabetically: be, to, then 'not' before 'or'.
More Python Programming questions
- What is printed? python class Stack(list): def push(self, v): self.append(v) return self s = Stack() s.push(1).push(2) t = s + [3]…
- What is printed? (Output lines are shown separated by " / ".) python def f(, a, b=2): return a - b def g(a, b=2, /): return a - b…
- What is printed? python def outer(fn): def w(a, k): return fn(a, k) 2 return w class C: @outer def val(self, x): return x + 1 @property…
- What is printed? python def log(prefix): def deco(fn): def w(a): return prefix + str(fn(a)) return w return deco @log("A:") @log("B:") def…
- What is printed? python import functools def once(fn): done = [] @functools.wraps(fn) def w(a): if not done: done.append(fn(a)) return…
- What is printed? python class A: def init(self): print("A", end=" ") class B(A): def init(self): print("B", end=" ") class C(B): pass C()…