ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
from collections import Counter
c = Counter('aab')
c.subtract('ab')
print(c['a'], c['b'], sorted((+c).items()))- A. 1 -1 [('a', 1), ('b', -1)]
- B. 0 -1 [('a', 0)]
- C. 1 0 [('a', 1)]
- D. 0 0 [('a', 1), ('b', 0)]
Correct answer: C. 1 0 [('a', 1)]
subtract lowers each count by one, leaving a at 1 and b at 0. Unary + drops entries whose count is zero or less, so only ('a', 1) remains.
More Python Programming questions
- What is printed? python n = 100 z, p = 0, 5 while p <= n: z += n // p p = 5 print(z, len(str(import('math').factorial(n)))…
- What is printed? python n, steps = 27, 0 while n != 1: n = n // 2 if n % 2 == 0 else 3 n + 1 steps += 1 print(steps, n)
- What is printed? python x, rev = 1200, 0 while x: rev = rev 10 + x % 10 x //= 10 print(rev, int(str(1200)[::-1]), str(1200)[::-1] == '0021')
- What is printed? python print(int('zz', 36), format(1295, 'x'), int('1295', 10) == 1295, int('zz', 36) == 35 36 + 35)
- What is printed? python print([n for n in range(100, 1000) if sum(int(d) 3 for d in str(n)) == n])
- What is printed? python print(bin(37).count('1'), bin(37)[2:].count('0'), (37).bitlength() - bin(37).count('1'))