ProgrammingHardCognizant GenC · TCS NQT
What is printed?
What is printed?
from collections import OrderedDict
od = OrderedDict()
for k in ['a', 'b', 'c']:
od[k] = ord(k)
od.move_to_end('a')
od['b'] = 0
od.popitem(last=False)
print(list(od.items()))- A. [('c', 99), ('a', 97)]
- B. [('a', 97), ('c', 99)]
- C. [('b', 0), ('c', 99), ('a', 97)]
- D. [('c', 99), ('a', 97), ('b', 0)]
Correct answer: A. [('c', 99), ('a', 97)]
move_to_end moves a to the back, and assigning to an existing key keeps its place. popitem(last=False) removes the oldest key, b, which leaves c and then a.
More Python Programming questions
- What is printed? python b = [3, 1, 2] a = b.sort() c = sorted(b, reverse=True) print(a, b, c)
- What is printed? python def kadane(a): best = cur = a[0] start = bestl = bestr = 0 for i in range(1, len(a)): if cur < 0: cur = a[i] start…
- What is printed? python def threesum(a): a = sorted(a) out = [] for i in range(len(a) - 2): if i and a[i] == a[i - 1]: continue l, r = i +…
- What is printed? python def distinct(s, t): dp = [1] + [0] len(t) for ch in s: for j in range(len(t), 0, -1): if t[j - 1] == ch: dp[j] +=…
- What is printed? python import bisect def pairsle(a, d): a = sorted(a) c = 0 for i, x in enumerate(a): j = bisect.bisectright(a, x + d, i…
- What is printed? python import bisect def closest(a, x): i = bisect.bisectleft(a, x) cands = a[max(i - 1, 0):i + 1] return min(cands…