ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
from collections import Counter
print(Counter('hello').most_common(2))- A. [('l', 2), ('o', 1)]
- B. [('l', 2), ('h', 1)]
- C. [('l', 2), ('e', 1)]
- D. [('h', 1), ('l', 2)]
Correct answer: B. [('l', 2), ('h', 1)]
Counter('hello') gives l:2, h:1, e:1, o:1. The most common value is l with 2. Ties among the count-1 letters go to the one seen first, which is h.
More Python Programming questions
- What is printed? python def keyfn(p): return (-p[1], p[0]) people = [('bo', 3), ('al', 5), ('cy', 3)] print([p[0] for p in sorted(people…
- What is printed? python s = 'abcabc' print(s.find('c'), s.find('c', 3), s.rfind('c', 0, 4), s.count('c', 2))
- What is printed? python nums = [1, 2, 3, 4, 5] print([nums[i] for i in range(len(nums) - 1, -1, -2)], nums[::-2][0])
- What is printed? python def walk(s): total = 0 for i, ch in enumerate(s, start=1): if ch.isupper(): total += i else: total -= i return…
- What is printed? python print(sum(x for x in range(1, 11) if x & 1), sum(x for x in range(1, 11) if x & 2))
- What is printed? python x = 'AbC' print(x.swapcase(), x.lower() < x.upper(), sorted(x) == ['A', 'C', 'b'])