ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
from collections import Counter c = Counter([3, 1, 3, 2, 1, 3]) print(c.most_common(1), sorted(c.elements()), c[7])
- A. [(3, 3)] [1, 1, 2, 3, 3] 0
- B. [(1, 2)] [1, 1, 2, 3, 3, 3] 0
- C. [(3, 3)] [1, 1, 2, 3, 3, 3] None
- D. [(3, 3)] [1, 1, 2, 3, 3, 3] 0
Correct answer: D. [(3, 3)] [1, 1, 2, 3, 3, 3] 0
Counter returns 0 for a missing key instead of raising KeyError, and most_common(1) gives the single most frequent value 3 with count 3. elements() repeats each value by its count, so sorting gives six items.
More Python Programming questions
- What is printed? python from math import comb print(comb(5, 2), comb(5, 7), comb(0, 0), comb(3, 1) comb(3, 1))
- What is printed? python x = 0 total = 0 for i in range(1, 6): if i % 2: continue x += i if x > 4: break else: total = -1 print(x, total)
- What is printed? python def f(n): if n <= 1: return n return f(n - 1) + f(n - 2) print(f(20), f.defaults)
- What is printed? python s = 'abcabc' print(s.index('c'), s.rindex('a'), s.find('bca', 1), s.count('bc', 1, 4))
- What is printed? python nums = [4, 9, 16] print(list(map(pow, nums, [0.5] 3)), [n 0.5 for n in nums])
- What is printed? python def gen(n): for i in range(n): yield i i g = gen(4) print(sum(g), sum(g), list(gen(0)))